인프런 김영한 님의 스프링 입문 - 코드로 배우는 스프링 부트, 웹, MVC, DB 접근 기술 강의를 보고 정리한 내용입니다.
웹 개발은 크게 3가지 방식으로 볼 수 있다.
/static(/public, /resources, /META-INF/resources) 경로로부터 정적 콘텐츠 기능을 제공한다.ResourceHttpRequestHandler를 사용하여 정적 리소스를 처리하고, ebMvcConfigurer를 추가해 addResourceHandlers 메서드를 오버라이드하여 동작을 수정할 수 있다.ServletContext 의 루트에서 콘텐츠를 제공하지만, 대부분의 경우 스프링이 DispatcherServlet을 통해 모든 요청을 처리한다./** 경로에 매핑되고, properties에 spring.mvc.static-path-pattern 속성을 사용해 경로를 지정할 수 있다.spring.mvc.static-path-pattern=/resources/**properties에 spring.resources.static-locations 속성을 사용해 정적 리소스 위치를 커스터마이징할 수 있다.
resources/static 경로에서 hello-static.html 파일을 찾음MVC: Model, View, Controller
- View: 화면을 그리는데 모든 역량을 집중
- Controller, Model: 비즈니스 로직, 내부적 처리에 집중

@RequestParam 값이 있는지 확인(required = true가 기본값이라)MissingServletRequestParameterException 반환Model에 RequestParam으로 받은 값 추가 후 반환ViewResolver (뷰를 찾아주고 템플릿 엔진 연결) 스프링 부트가 resources/tempaltes 경로에서 return의 StringName(ViewName) 파일을 찾음@GetMapping("hello-string")
@ResponseBody // http body에 데이터를 직접 넣겠다
public String helloString(@RequestParam("name") String name) {
return "hello " + name;
}
@ResponseBody 를 사용하면 ViewResolver를 사용하지 않음
@GetMapping("hello-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;
}
}
@ResponseBody 를 사용하고, 객체를 반환하면 객체가 JSON으로 변환됨

@ResponseBody Annotation을 확인HttpMessageConverter가 작동StringConverter(StringHttpMessageConverter)가 return String Value 그대로 JsonConverter(MappingJackson2HttpMessageConverter)가 객체를 json 형식의 데이터로 만듬@ResponseBody 를 사용viewResolver 대신에 HttpMessageConverter가 동작StringHttpMessageConverterMappingJackson2HttpMessageConverter💡 Jackson: 범용적으로 사용되는 객체 → json 스타일 변환 라이브러리