spring boot 配置LocalDate, LocalDateTime

gradle配置

    compile("org.mybatis:mybatis-typehandlers-jsr310:1.0.2")
    compile 'com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.5'
    compile('com.fasterxml.jackson.core:jackson-core:2.9.5')
    compile('com.fasterxml.jackson.core:jackson-databind:2.9.5')
    compile('com.fasterxml.jackson.core:jackson-annotations:2.9.5')
    compile 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.5'
    compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.5")

mvn 配置


			com.alibaba
			fastjson
			1.2.46
		
		
		
			com.fasterxml.jackson.datatype
			jackson-datatype-jsr310
		
		
			com.fasterxml.jackson.datatype
			jackson-datatype-jdk8
		
		
			com.fasterxml.jackson.module
			jackson-module-parameter-names
		
		
			com.fasterxml.jackson.core
			jackson-core
		
		
			com.fasterxml.jackson.core
			jackson-databind
		
		
			com.fasterxml.jackson.core
			jackson-annotations
		
		
			org.mybatis
			mybatis-typehandlers-jsr310
			1.0.2
		

Spring Boot 2.x配置模板

@Configuration
public class WebMvcByFastjson implements WebMvcConfigurer {

    @Bean
    public HttpMessageConverter fastJsonHttpMessageConverter() {
        // 1.需要先定义一个convert 转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        // 2.添加fastJson的配置信息,比如,是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        fastJsonConfig.setSerializeFilters((ValueFilter) (o, s, source) -> {
            if (source instanceof LocalDateTime) {
                return DateUtil.dateTimeAsString((LocalDateTime)source,"yyyy-MM-dd HH:mm:ss");
            }
            return source;
        });
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.BrowserCompatible,
                SerializerFeature.WriteNullStringAsEmpty
        );

        // 处理中文乱码问题
        List fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);

        // 3.在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        return fastConverter;
    }

    // 配置消息转换器
    @Override
    public void configureMessageConverters(List> converters) {
        converters.add(fastJsonHttpMessageConverter());

    }

}
``````````````````````



Spring Boot学习之MVC与Validation
https://yangbingdong.com/2018/spring-boot-mvc-validation/

 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(技术,spring,boot,fastjson,LocalDate,LocalDateTime,序列化,反序列化)