1. 정적 컨텐츠
-스프링 부트가 제공하는 정적 컨텐츠 기능
-resources/static에 mye-static.html 파일 생성
-static content 정적 컨텐츠
<!DOCTYPE HTML>
<html>
<head>
<title>static content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
정적 컨텐츠 입니다.
</body>
</html>
-실행
http://localhost:8080/mye-static.html 접속
-정적 컨텐츠 이미지
2. MVC와 템플릿 엔진
-resources/templates에 mye-template.html 생성
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
-실행
http://localhost:8080/mye-mvc
-mye-controller에서 value="name"으로 수정
-http://localhost:8080/mye-mvc?name=spring 로 접속
-MVC, 템플릿 엔진 이미지
웹브라우저-내장 톰캣서버-helloController-viewResolver-HTML 변환 후 웹브라우저에 넘겨줌
3. API
(1) hello-string
-RespondeBody 문자반환
-실행화면
http://localhost:8080/mye-string?name=spring!
-> 소스 코드를 확인해보면 별도 html 없이 string 값만 존재하는 것 확인 가능
-@ResponseBody 를 사용하면 뷰 리졸버( viewResolver )를 사용하지 않음
-HTTP의 BODY에 문자 내용을 직접 반환(HTML BODY TAG를 말하는 것 x)
(2) hello-api
@Controller
public class myeController {
@GetMapping("mye-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) {
Hello hello = new Hello();
hello.setName(name);
return hello;
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
}
-실행 시 json 형식으로 출력됨
http://localhost:8080/mye-api?name=spring!
- @ResponseBody 를 사용, 객체를 반환하면 객체가 JSON으로 변환됨
(3) 동작원리
- 스프링에서 helloController를 찾아감
- @ResponseBody가 없으면? ViewResolver가 나에게 맞는 템플릿 찾아서 돌려줘
- @ResponseBody가 있다면? http의 BODY에 문자 내용을 직접 반환 | 객체라면 디폴트를 JSON 형식으로 만들어서!!
- JSONConverter(기본 객체 처리) | StringConverter(기본 문자 처리)
'Programming Study > Spring' 카테고리의 다른 글
[Inflearn] 스프링 입문 | 3 회원 관리 예제 - 백엔드 개발 (0) | 2023.01.01 |
---|---|
[Inflearn] 스프링 입문 | 1 프로젝트 환경설정 (0) | 2022.12.30 |