SpringMVC返回json将null转为空字符串 作者:马育民 • 2021-10-11 10:27 • 阅读:10067 # 说明 需要 jackson jar包或依赖 # java代码 ``` package com.im.json; 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 java.io.IOException; public class JsonObjectMapper extends ObjectMapper { private static final long serialVersionUID = 1L; public JsonObjectMapper() { super(); // 空值处理为空串 this.getSerializerProvider().setNullValueSerializer(new JsonSerializer() { @Override public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException { jg.writeString(""); } }); } } ``` # spring-web 增加下面内容: ``` ``` 原文出处:http://malaoshi.top/show_1IX21MS8yEsA.html