날짜/시간은 브라우저에서 문자열로 전송되지만, 서버에서는 LocalDate 혹은 LocalDateTime으로 처리된다. 그렇기 때문에 이를 변환해 주는 Formatter를 추가해서 이 과정을 자동으로 할 수 있도록 설정한다.
public class LocalDateFormatter implements Formatter<LocalDate> {
@Override
public LocalDate parse(String text, Locale locale) throws ParseException {
return LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
@Override
public String print(LocalDate object, Locale locale) {
return DateTimeFormatter.ofPattern("yyyy-MM-dd").format(object);
}
}
@Configuration을 통해 등록해준다.
@Configuration
@Log4j2
public class CustomServletConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
log.info("------------------------------------");
log.info("addFormatters");
registry.addFormatter(new LocalDateFormatter());
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 어떤 경로에 적용할지
.allowedOrigins("*") // 어디에서 오는 연결 허락할 것인가
.allowedMethods("HEAD", "GET", "POST", "PUT", "DELETE", "OPTIONS")
.maxAge(300)
.allowedHeaders("Authorization", "Cache-Control", "Content-Type");
}
}
또한 Ajax를 이용해서 서비스를 호출하게 되면 반드시 '교차 출처 리소스 공유'로 인해 호출이 제한된다. 리액트에서 스프링부트로 동작하는 서버를 호출하야 하므로 CustomServletConfig에 추가적인 설정을 해주었다.