SPRINGBOOT返回数据NULL参数设为空字符串或空数组

https://www.cnblogs.com/jthr/p/15825435.html
SPRINGBOOT返回数据NULL参数设为空字符串或空数组
package com.ruoyi.framework.config.ResponseVoConfig.WebConfig; 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 com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import java.io.IOException; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.List; import java.util.Map; import java.util.Objects; import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; public class MyJsonMapper extends ObjectMapper { public MyJsonMapper() { super(); //收到未知属性时不报异常 this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false); //Long类型转为String类型 SimpleModule simpleModule = new SimpleModule(); //simpleModule.addSerializer(Long.class, ToStringSerializer.instance); //simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); this.registerModule(simpleModule); this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); //设置日期格式 //处理空指针时设置的值 this.getSerializerProvider().setNullValueSerializer(new JsonSerializer
package com.ruoyi.framework.config.ResponseVoConfig.WebConfig; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessa @Configuration public class WebConfig extends WebMvcConfigurationSuppogeConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSuppo rt; import java.util.List; rt { @Override public void configureMessageConverters(List> converters) { //在json转换之前先进行string转换 converters.add(new StringHttpMessageConvert er()); //添加json转换 MappingJackson2HttpMessageConverter jackson2HttpMessageConve rter = new MappingJackson2HttpMessa geConverter(); jackson2HttpMessageConve new MyJsonMapper()); converters.add(jackson2HttpMessageConverter.setObjectMapper( rter); //5、追加默认转换器 super.addDefaultHttpMessageConverters(converters); } }
分类: springboot