将数据中的null 替换成""

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.
 */
@Component
public class JsonObjectMapper extends ObjectMapper {
    public JsonObjectMapper() {
        super();
        // 空值处理为空串
        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
            public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {
                jg.writeString("");
            }
        });
    }
}

调用:
return new JsonObjectMapper().writeValueAsString(Object);

你可能感兴趣的:(将数据中的null 替换成"")