SpringBoot使用Jackson序列化反序列化配置

几种常见方式

1. 全局application配置

常用配置:

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    serialization: true
    deserialization: true
    default-property-inclusion: non_empty
    parser: true

具体配置详见org.springframework.boot.autoconfigure.jackson.JacksonProperties

2. 重新注入ObjectMapper

Jackson配置:
@ConditionalOnClass(ObjectMapper.class)
public class JacksonAutoConfiguration {…}

例:

    @Bean
    @Primary
	@ConditionalOnMissingBean(ObjectMapper.class)
	public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
		ObjectMapper objectMapper = builder.createXmlMapper(false).build();
		// 属性为”“或null时不序列化。
		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
		// 允许未知属性
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
		// 字段保留, 讲null转为”“
		objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
			@Override
			public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
				gen.writeString("");
			}
		});
		return objectMapper;
	} 
  

3. 自定义MappingJackson2HttpMessageConverter

    @Bean
	public MappingJackson2HttpMessageConverter converter() {
		MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
		ObjectMapper objectMapper = new ObjectMapper();
		JavaTimeModule timeModule = new JavaTimeModule();
		timeModule.addSerializer(Date.class, new DateSerializer(false, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
		timeModule.addDeserializer(Date.class, new DateDeserializers.DateDeserializer());
		timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		objectMapper.registerModule(timeModule);
		objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
		objectMapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);	// 未知属性
		objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
		objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES, false);
		messageConverter.setObjectMapper(objectMapper);
		return messageConverter;
	}

 

附: 使用RestTemplate调用外部接口序列化配置

注入上面配置好的MappingJackson2HttpMessageConverter再构造template即可。

    @Resource
	private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;    

    @Bean
	public RestTemplate restTemplate() {
		// 使用restTemplate调用外部接口使用自定义配置的jackson序列化和反序列化。 (忽略不存在的字段,忽略属性大小写)
		return new RestTemplate(Collections.singletonList(mappingJackson2HttpMessageConverter));
	}

 

你可能感兴趣的:(java,java,spring)