정적 컨텐츠는 만들어진 파일 그대로 사용자에게 전달된다.
[ex] resources/static/hello-static.html
<!DOCTYPE HTML>
<html>
<head>
<title>static content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
정적 컨텐츠 입니다.
</body>
</html>
[ex] 실행

[ex. Controller] java/hello.hellospring/controller/HelloController
@GetMapping("hello-mvc")
//이 메서드는 웹사이트에서 url을 파라미터로 받아오도록 해준다
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
?name=spring화이팅 와 같이 해주어야 한다.[ex. View] resources/template/hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
[ex] 실행

컨트롤러에서 Get으로 매핑된 hello-mvc 메서드를 찾아 실행시킨다.✅ 정적 컨텐츠를 제외한다면?
- MVC 방식
원하는 view를 찾은 후, 템플릿 엔진을 통해서 해당 view를 html로 렌더링해서 웹 브라우저에게 넘겨주는 방식
- API 방식
JSON 형식으로 바꾸어 클라이언트에게 데이터를 전달하는 방식
view없이 그대로 http의 body에 전달하는 방식
- Template engine은 view라는 템플릿이 있는 상황에서 view를 html로 변환하는 것이고,
API는 데이터를 그대로 내보내는 것이다.
[ex. @ResponseBody 문자 반환] java/hello.hellospring/controller/HelloController.java
@GetMapping("hello-string")
@ResponseBody //http(통신 프로토콜)에서 헤더부와 바디부가 있는데, 그 바디에 return(반환)값을 "직접" 넣어주겠다는 뜻.
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
@ResponseBody를 사용하면 뷰 리졸버(viewResolver)를 사용하지 않는다.[ex] 실행
[ex. @ResponseBody 객체 반환] java/hello.hellospring/controller/HelloController.java
@GetMapping("hello-api")
@ResponseBody //객체가 오면 > json 반환
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으로 변환된다.[ex] 실행

@ResponseBody 를 사용
🔗 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술
🔗 https://dmaolon00.tistory.com/121