提交 e0497820 authored 作者: lvwei's avatar lvwei

--no commit message

上级 0013af9a
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
package com.zrqx.commons.convert;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.core.convert.converter.Converter;
/**
* 字符串转换为Date
* @author lpf
* @date 2018年7月10日下午4:02:01
*/
public class StringToDateConverter implements Converter<String, Date> {
private static final List<String> formarts = new ArrayList<String>(4);
static{
formarts.add("yyyy-MM");
formarts.add("yyyy-MM-dd");
formarts.add("yyyy-MM-dd hh:mm");
formarts.add("yyyy-MM-dd HH:mm:ss");
}
@Override
public Date convert(String source) {
String value = source.trim();
if ("".equals(value)) {
return null;
}
if(source.matches("^\\d{4}-\\d{1,2}$")){
return parseDate(source, formarts.get(0));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
return parseDate(source, formarts.get(1));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(2));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(3));
}else {
throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
}
}
public Date parseDate(String dateStr, String format) {
Date date=null;
try {
DateFormat dateFormat = new SimpleDateFormat(format);
date = (Date) dateFormat.parse(dateStr);
} catch (Exception e) {
}
return date;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论