美文网首页
使用Jackon作为JSON MessageConverter

使用Jackon作为JSON MessageConverter

作者: I_Gisvity | 来源:发表于2019-06-10 11:26 被阅读0次

    使用Jackon作为JSON MessageConverter

    
    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.databind.JsonSerializer;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializerProvider;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
    
    import java.io.IOException;
    
    /**
     * 使用Jackon作为JSON MessageConverter
     * 对null处理为""
     *
     * @author XuHao
     * Email 15229357319@sina.cn
     * create on 2018/7/29
     */
    
    @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<Object>() {
                @Override
                public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                    jsonGenerator.writeString("");
                }
            });
            return objectMapper;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:使用Jackon作为JSON MessageConverter

          本文链接:https://www.haomeiwen.com/subject/pytjxctx.html