HTTP 요청 종류 (서버클라이언트 데이터 전달 방식)

1. 정적 콘텐츠

- css, js, 이미지 등

 

2. 뷰 템플릿

 

3. HTTP 응답 메시지

- HTTP API를 제공하는 경우 json 등의 데이터만 HTTP body에 담아서 전달

 


 

1. 정적 콘텐츠

- 해당 파일을 변경 없이 그대로 제공

- /static, /public, /resources, /META-INF/resources

- 정적 리소스 경로 : src/main/resources/static

ex) http://localhost:8080/basic/hello.html => src/main/resources/static/basic/hello.html

 

2. 뷰 템플릿

- 뷰 템플릿 경로 : src/main/resources/templates

- @ResponseBody가 없으면 뷰 리졸버를 실행해서, 반환 String값으로 뷰를 찾고 렌더링

- @ResponseBody가 있으면 뷰 리졸버를 실행하지 않고, 반환 String값을 HTTP body에 넣어서 전달

- Thymeleaf => 스프링 부트가 ThymeleafViewResolver를 빈으로 등록하고, application.properties도 기본으로 설정

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

=> 논리뷰명 : hello → 물리뷰명 templates/hello.html

 

3. HTTP 응답 메시지

- HTTP API 제공하는 경우 HTML대신 데이터를 전달함

- HTTP body에 json과 같은 형식의 데이터를 넣어서 전달함

 

response.getWirter().write("ok");

 

ResponseEntity

- HttpEntity를 상속 받음

@GetMapping("/response")
public ResponseEntity<String> responseEntityTest() {
	return new ResponseEntity<>("ok", HttpStatus.OK);
}

 

 

@ResponseBody

@ResponseStatus("HttpStatus.OK")
@ReponseBody
@GetMapping("/response")
public HelloData responseBodyTest() {
	HelloData helloData = new HelloData();
    helloData.setUserName = "AAA";
    helloData.setAge = "20";
    return helloData;
}

 

@RestController

- @Controller대신 @RestController를 붙이면 @ResponseBody가 전체 메서드에 적용됨

- REST API를 만들 때 사용하는 컨트롤러