业务返回json格式,如果为null,要返回空字符串“”。
网上都是要注入一个ObjectMapper
@Configuration
public class ResultConfig {
@Bean
@Primary
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString("");
}
});
return objectMapper;
}
可是还是不能覆盖掉 jacksonAutoConfig 配置的 ObjectMapper?这是为什么?
@Bean
@Primary
@ConditionalOnMissingBean
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build();
}
@ConditionalOnMissingBean 不是应该没有这个bean才会生效吗?我已经注入了为什么默认的还会生效?