提交 5753efa4 authored 作者: lvwei's avatar lvwei

--no commit message

上级 e9972561
package com.zrqx.commons.config;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.zrqx.commons.convert.StringToDateConverter;
import com.zrqx.core.commons.interceptor.InterceptorConfig;
import com.zrqx.core.constant.sysuser.SysUserRequestPath;
/**
* 使用CustomWebMvcConfigurationSupport来显对 mvc的配置
* @author lpf
* @date 2018年7月24日下午8:01:05
*/
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
/**
* 跨域支持
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").allowCredentials(true).maxAge(3600);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册自定义拦截器,添加拦截路径和排除拦截路径
registry.addInterceptor(new InterceptorConfig()).addPathPatterns(SysUserRequestPath.BG + "/**")
.excludePathPatterns(SysUserRequestPath.BG + SysUserRequestPath.PERMISSIONS + SysUserRequestPath.LOGIN);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(fastJsonHttpMessageConverter());
super.configureMessageConverters(converters);
}
@Bean
public HttpMessageConverter<Object> fastJsonHttpMessageConverter(){
// fastjson 转换器
FastJsonHttpMessageConverter fast = new FastJsonHttpMessageConverter();
// fastjson 配置
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat, SerializerFeature.QuoteFieldNames);
fast.setFastJsonConfig(fastJsonConfig);
// fastJson 支持解析类型
List<MediaType> list = new ArrayList<MediaType>();
list.add(MediaType.APPLICATION_JSON_UTF8);
fast.setSupportedMediaTypes(list);
return fast;
}
@Bean
public Converter<String, Date> stringToDateConverter() {
StringToDateConverter conver = new StringToDateConverter();
return conver;
}
@Bean
public FormattingConversionServiceFactoryBean conversionService() {
FormattingConversionServiceFactoryBean bean = new FormattingConversionServiceFactoryBean();
Set<Converter<?, ?>> set = new HashSet<Converter<?, ?>>();
set.add(stringToDateConverter());
bean.setConverters(set);
return bean;
}
public ConfigurableWebBindingInitializer webBindingInitializer(@Autowired ConfigurableWebBindingInitializer config) {
config.setConversionService(conversionService().getObject());
return config;
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论