spring boot 返回json字符串 null值转空字符串

spring boot 返回json字符串 null值转空字符串


@Configuration
public class JacksonConfig {
    @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, JsonProcessingException {
                jsonGenerator.writeString("");
            }
        });
        return objectMapper;
    }
} 
  

将代码复制到项目中扫描到就可以了,返回的只就会从“null”变成“”


enjoy this code.


转载自:https://blog.csdn.net/sidan8/article/details/62888035

你可能感兴趣的:(转载)