SpringMVC Jackson为null转化为空串处理

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;

/**
 * @author Dongguabai
 * @date 2018/10/19 17:13
 */
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("");
            }
        });
    }
}
 
  

SpringMVC配置文件:

 
        
            
                text/html;charset=UTF-8
            
        
        
            
        
    

其实这里方法有很多,可以自行扩展:

SpringMVC Jackson为null转化为空串处理_第1张图片

参考资料:

https://www.cnblogs.com/cvst/p/5993646.html

你可能感兴趣的:(serialize,spring,mvc)