将数据中的null 替换成""

来源:互联网 发布:nx三维软件 编辑:程序博客网 时间:2024/06/09 18:55
package com.lesiea.util;
import java.io.IOException;import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.JsonSerializer;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializerProvider;import org.springframework.stereotype.Component;/** * Created by Duo Nuo on 2016/7/13. */@Componentpublic class JsonObjectMapper extends ObjectMapper {    public JsonObjectMapper() {        super();        // 空值处理为空串        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {            public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {                jg.writeString("");            }        });    }}调用:
return new JsonObjectMapper().writeValueAsString(Object);

0 0
原创粉丝点击