본문 바로가기
스프링 부트 3으로 백엔드 입문하기

2장 스프링 부트 3 시작하기

by hoshi03 2023. 11. 21.

• 스프링 부트 3, 자바 17 기능들

 

텍스트 블록 -  """ 안에 여러 줄의 텍스트 ex) 쿼리문을 넣을 수 있다

formatted 메서드 json을 파싱할떄

String textBlock = """
{
	"id" : %d
    "name" : %s
}
""".formatted(2, "juice);

형태로 함수 하나로 파싱할 수 있다

 

레코드

 

데이터 전달 객체를 빠르고 간편하게 만든다

레코드는 상속이 안되고 파라미터에 정의한 필드는 private final로 정의된다

레코드는 getter를 자동으로 만든다

record Item(String name, int price){
	//여기 들어가는 파라미터는 private final로 정의된다
}

Item juice = new Item("juice", 3000);
juice.price();

 

패턴 매칭

 

타입 확인할때 사용하던 instanceof를 좀 편하게 사용할 수 있다

 

if (o instanceof Integer){
	// 이 형변환 코드를 생략해도 된다
    Integer i = (Integer) o;
}

 

 

스프링 부트 3 코드 이해하기

@SpringBootApplication 이해하기

@SpringBootApplication
public class SpringbootDeveloperApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootDeveloperApplication.class, args);
    }
}

 

SpringApplication.run(SpringbootDeveloperApplication.class, args);

run 메서드는 메인 클레스로 사용할 클래스, 커멘드 라인을 받아서 어플리케이션을 실행한다

 

@SpringBootApplication 어노테이션 

스프링 부트에 필요한 기본 설정을 한다

 

@SpringBootApplication 어노테이션을 구성하는 어노테이션 중 아래 어노테이션들을 자세히 보자

@EnableAutoConfiguration
@ComponentScan

@ComponentScan 어노테이션

 @Component 어노테이션을 가진 클래스를 찾아서 빈으로 등록한다

 

@EnableAutoConfigurarion 어노테이션

스프링 부트에서 자동 구성을 활성화한다

 

테스트 컨트롤러 살펴보기

@RestController
public class TestController {
    @GetMapping("/test")
    public String test(){
        return "Hello, world!";
    }
}

 

@RestController : @Controller에 @ResponseBody를 추가한 것,json 결과값을 받을 수 있다

라우터 역할을 해서 /test라는 get 요청이 왓을때 test() 메서드를 실행한다

'스프링 부트 3으로 백엔드 입문하기' 카테고리의 다른 글

6장 API 만들기  (0) 2023.11.23
5장 ORM  (0) 2023.11.22
4장 스프링 부트 3 테스트  (1) 2023.11.22
3장 스프링 부트 3 구조 이해하기  (1) 2023.11.21
1장 스프링 기초 지식  (0) 2023.11.21