사용자 도구

사이트 도구


springframework:springboot:mvc

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
springframework:springboot:mvc [2018/07/20 11:23]
kwon37xi [Formatter]
springframework:springboot:mvc [2020/07/17 10:21] (현재)
kwon37xi [Converter]
줄 1: 줄 1:
-====== SpringBoot and Spring MVC ======+====== SpringBoot and Spring Web MVC ======
 ===== 설정 커스터마이즈 ===== ===== 설정 커스터마이즈 =====
   * [[https://docs.spring.io/spring/docs/4.3.x/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.html|WebMvcConfigurerAdapter]] 를 구현하고 ''@Configuration''을 등록하면 여러가지 옵션을 override 할 수 있다.   * [[https://docs.spring.io/spring/docs/4.3.x/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.html|WebMvcConfigurerAdapter]] 를 구현하고 ''@Configuration''을 등록하면 여러가지 옵션을 override 할 수 있다.
줄 7: 줄 7:
   * ''@EnableWebMvc''를 활성화 하면 일반 Spring MVC 모드가 되어 Auto Configuration이 안먹게 된다.   * ''@EnableWebMvc''를 활성화 하면 일반 Spring MVC 모드가 되어 Auto Configuration이 안먹게 된다.
     * 이 경우 ''WebMvcConfigurationSupport'' Bean이 등록되는데 이 Bean이 존재하면 Auto Configuration 이 중단된다.     * 이 경우 ''WebMvcConfigurationSupport'' Bean이 등록되는데 이 Bean이 존재하면 Auto Configuration 이 중단된다.
 +    * 즉, SpringBoot 에서는 ''org.springframework.boot:spring-boot-starter-web'' 의존성이 걸려있다면 ''@EnableWebMvc''를 설정하지 말라.
 +
 +===== @WebMvcTest 가 잘되게 하려면 =====
 +  * [[springframework:springboot:test|SpringBoot Test]]의 ''@WebMvcTest''는 WebMvc 관련 설정만 읽고 나머지 컴포넌트에 대한 설정은 읽지 않는데, 이 때문에 WebMvc 관련 설정(''WebMvcConfigurer'')가 다른 컴포넌트를 import 하는 설정들과 섞여 있으면 컨텍스트를 올바로 설정하지 못한다.
 +  * 따라서 WebMvc 관련 설정(특히 ''WebMvcConfigurer'')들은 다른 컴포넌트들의 설정과 분리해 두도록 한다.
  
 ===== Formatter ===== ===== Formatter =====
-  * Controller 파라미터로 들어온 문자열을 객체로 변환해주는 전역 설정.+  * Controller 파라미터로 들어온 **문자열을 객체로 변환**해주는 전역 설정
 +  * [[https://docs.spring.io/spring/docs/5.0.x/javadoc-api/org/springframework/format/Formatter.html|Formatter]] 인터페이스 구현. ''Formatter''의 상속 클래스들도 모두 살펴볼 것.
   * 날짜의 경우 ''@DateTimeFormat'' 애노테이션으로 모든 파라미터마다 설정해주는 것도 가능하지만 공통 전역 설정으로 ''Date'', ''LocalDate'', ''LocalDateTime'' 을 만들어두는게 좋음.   * 날짜의 경우 ''@DateTimeFormat'' 애노테이션으로 모든 파라미터마다 설정해주는 것도 가능하지만 공통 전역 설정으로 ''Date'', ''LocalDate'', ''LocalDateTime'' 을 만들어두는게 좋음.
-  * [[https://docs.spring.io/spring/docs/5.0.x/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.html|WebMvcConfigurer]] 로 등록. +  * [[https://docs.spring.io/spring/docs/5.0.x/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.html|WebMvcConfigurer]] ''addFormatters''로 등록. 
-===== Converter =====+  * Formatter와 Jackson Serializers/Deserializers 에 대해서 테스트를 만들어 보증하는 것이 좋다. [[https://gist.github.com/kwon37xi/aa359e364b7e81f79085fb04cc710037|FormatterSerializerTestControllerTest.groovy]]
  
 +==== Java 8 java.time.* Formatter 일괄 등록 ====
 +  * from https://stackoverflow.com/a/47934151/1051402
 +  * 아래 설정은 Java 8 ''java.time.*'' 의 Formatter 를 일괄등록해준다.
  
 +<code java>
 +@Configuration
 +public class MyApiWebMvcConfig  implements WebMvcConfigurer {
 +
 +    @Override
 +    public void addFormatters(FormatterRegistry registry) {
 +        DateTimeFormatterRegistrar dateTimeFormatterRegistrar = new DateTimeFormatterRegistrar();
 +        dateTimeFormatterRegistrar.setUseIsoFormat(true); // ISO 포맷 사용시, 그게 아니면 각자 명시적 설정
 +        dateTimeFormatterRegistrar.registerFormatters(registry);
 +    }
 +}
 +</code>
 +
 +===== Converter =====
 +  * [[springframework:springboot:httpmessageconverters|SpringBoot HttpMessageConverters]]
springframework/springboot/mvc.1532053414.txt.gz · 마지막으로 수정됨: 2018/07/20 11:23 저자 kwon37xi