spring controller会将放回参数中null值默认不放回,可参考如下设置,注意如果application.properties中已设置#spring.jackson.default-property-inclusion=NON_NULL,则函数配置不生效;
```
/**
* @Description 控制返回的值为null的情况替换为空字符串/NULL
* @Author
* @Date 2018/8/29 17:19
**/
@Configuration
public class ResultConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
// jsonGenerator.writeString("");//替换字符串
jsonGenerator.writeNull();//以NUll替代
}
});
return objectMapper;
}
}
```
网友评论