정적 컨텐츠
: 웹 브라우저에 컨텐츠(정적 파일) 그대로 전달 → 관련 controller 필요 X
MVC와 템플릿 엔진
: Model, View - 화면 출력에 중점, Controller: Model의 비지니스 로직, 내부 처리
→ jsp가 처리
API
: json 데이터 포맷을 이용하여 데이터 전달, 서버끼리 통신 할 때
MVC : Model, View, Controller
: 렌더링된 html을 전달
/hello.hellospring.Controller/HelloController.java
@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String hello(@RequestParam(value = "name", required = false)String name, Model model){
model.addAttribute("name",name);
return "hello-mvc";
}
}
→ http://localhost:8000/hello-mvc?name=spring
InteliJ에서 'command + p' 필요한 파라미터 알려주는 단축키
: json 객체를 전달
/hello.hellospring.Controller/HelloController.java
@GetMapping("hello-api")
@ResponseBody
// Hello 객체 return(json)
public Hello helloApi(@RequestParam(value = "name", required = false)String name, Model model){
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 Data type으로 return
command + N : getter & setter
viewResolver
대신 HttpMessageConverter
가 동작MappingJackson2HttpMessageConverter
StringHttpMessageConverter