赞
踩
用JAVA进行编写WEB项目时,我们一般需要对WEB进行统一配置,例如制定拦截路径、页面解析器、跨域配置、fastjson报文解析、文件上传大小配置等。
- @Getter
- @Setter
- @Configuration
- public class WebConfiguration extends WebMvcConfigurationSupport {
-
- private static final String MATCH_ALL = "/**";
-
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(new RequestIdInterceptor()).addPathPatterns(MATCH_ALL);
- registry.addInterceptor(new MockRequestEntityInterceptor()).addPathPatterns(MATCH_ALL);
- }
-
- @Override
- public void addViewControllers(ViewControllerRegistry registry) {
- registry.addViewController("/").setViewName("index");
- registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
- super.addViewControllers(registry);
- }
-
- /*
- * Spring mvc 的页面解析器配置
- */
- @Bean
- public InternalResourceViewResolver viewResolver(){
- InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- viewResolver.setPrefix("/");
- viewResolver.setSuffix(".html");
- return viewResolver;
- }
-
- /**
- * 跨域配置
- */
- public CorsFilter corsFilter() {
- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
- source.registerCorsConfiguration("/**", corsConfig());
- return new CorsFilter(source);
- }
-
- /*
- * Spring mvc 的静态资源路径配置
- */
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/resource/**")
- .addResourceLocations("classpath:/static/resource/");
-
- registry.addResourceHandler("/*.html")
- .addResourceLocations("classpath:/static/");
- }
-
- /*
- * 项目序列化使用fastjson,因序列化解析器执行是列表由前至后的优先级,故把fastjson加入到序列0的位置
- */
- @Override
- public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
- converters.set(0,fastJsonHttpMessageConverter());
- }
-
- /*
- * 生成FastJsonHttpMessageConverter返回值解析器
- */
- public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
- FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
- converter.setFastJsonConfig(fastJsonConfig());
- converter.setSupportedMediaTypes(mediaTypes());
- return converter;
- }
-
- /*
- * 生成FastJsonHttpMessageConverter返回值解析器 - 解析详细配置
- */
- public FastJsonConfig fastJsonConfig() {
- FastJsonConfig fastJsonConfig = new FastJsonConfig();
- fastJsonConfig.setSerializerFeatures(
- SerializerFeature.PrettyFormat,
- SerializerFeature.WriteEnumUsingToString
- );
-
- // 日期时间格式及字符集配置
- fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
- fastJsonConfig.setCharset(Charset.forName("utf-8"));
-
- // 防止Long类型转json丢失精度的问题
- SerializeConfig serializeConfig = SerializeConfig.globalInstance;
- serializeConfig.put(Long.class, ToStringSerializer.instance);
- serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
- fastJsonConfig.setSerializeConfig(serializeConfig);
- return fastJsonConfig;
- }
-
- /*
- * 生成FastJsonHttpMessageConverter返回值解析器 - 解析格式组件 支持的mediaType的类型
- * 1.application/json;charset=UTF-8
- * 2.application/json
- */
- public List<MediaType> mediaTypes() {
- List<MediaType> mediaTypeList = new ArrayList<>();
- mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
- mediaTypeList.add(MediaType.APPLICATION_JSON);
- return mediaTypeList;
- }
-
- @Bean
- public CommonsMultipartResolver multipartResolver() {
- CommonsMultipartResolver resolver = new CommonsMultipartResolver();
- resolver.setDefaultEncoding("UTF-8");
- //resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
- resolver.setResolveLazily(true);
- resolver.setMaxInMemorySize(40960);
- //上传文件大小 5M 5*1024*1024
- resolver.setMaxUploadSize(5 * 1024 * 1024);
- return resolver;
- }
-
- private CorsConfiguration corsConfig() {
- CorsConfiguration corsConfiguration = new CorsConfiguration();
-
- corsConfiguration.addAllowedOrigin("*");
- corsConfiguration.addAllowedHeader("*");
- corsConfiguration.addAllowedMethod("*");
- corsConfiguration.setAllowCredentials(true);
- corsConfiguration.setMaxAge(3600L);
- return corsConfiguration;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。