前言
Spring boot做了很多默認(rèn)自動配置的功能??梢栽?code>spring-boot-autoconfigure中
在web/servlet/WebMvcAutoConfiguration文件中。
我們也可以自定義一些Handler,Interceptor,ViewResolver,MessageConverter等來自定義配置
@Configuration
public class WebConfig implements WebMvcConfigurer {
/**
* 添加類型轉(zhuǎn)換器和格式化器
* @param registry
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldType(LocalDate.class, new USLocalDateFormatter());
}
/**
* 跨域支持
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT")
.maxAge(3600 * 24);
}
}