引言
好久没记录了,记录一下 JacksonJson 常用的注解配置吧
注解
有时候返回对象里有 null 值或者为 “” 值的,不想返回这种字段可以在对象上用上这个注解
1
| @JsonInclude(JsonInclude.Include.NON_NULL)
|
Bean 配置
想整个项目统一处理的话,配置一个 Configuration 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeString(""); } }); return objectMapper; } }
|