본문 바로가기

Spring Boot

Chapter1 - 프로젝트 생성

1. Spring 프로젝트 생성

  • Spring Initializer를 활용하면, 쉽게 Spring 프로젝트를 생성 할 수 있습니다.
  • 목적과 환경에 맞춰 아래와 같이 설정 후 프로젝트를 생성하였습니다.

 

2. Sample API

  • FilmController.java를 아래와 같이 작성하였습니다.
package com.example.filmservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class FilmController {

    @Autowired
    private FilmService filmService;

    @GetMapping("/hello")
    public String sayHello() {
        return filmserviceService.sayHello();
    }
}
 @RestController  Spring MVC 컨트롤러 클래스를 의미
 @RequestMapping()  메서드 또는 클래스 레벨에서 URL 매핑을 지정
 @Autowired  의존성 주입을 수행함.
 주로 생성자, setter 등에 적용되어, 해당 클래스가 필요로 하는 다른 빈(Bean)을 자동으로 주입
 @GetMapping()  HTTP GET 요청에 대한 핸들러 메서드

 

  • FilmService.java를 아래와 같이 작성하였습니다.
package com.example.filmservice;

import org.springframework.stereotype.Service;

@Service
public class FilmService {
    public String sayHello() {
        return "Hello, World!";
    }
}
 @Service  클래스가 비즈니스 로직이나 서비스 역할을 수행함을 의미.
 스프링이 해당 클래스를 스캔하고 빈으로 등록하여 IoC 컨테이너에서 관리

 

'Spring Boot' 카테고리의 다른 글

Chapter2 - DB 연결(JPA)  (0) 2023.12.05