可以通过mvc解决,试着用反射做了一下
public class ChangeNullController {
public static void main(String[] args) {
Object o = new ChangeNullController().getUsers();
long start = System.nanoTime();
Object replace = replace(o, new HashMap<>());
System.out.println("执行耗时:" + (System.nanoTime() - start));
System.out.println(replace);
}
public List<User> getUsers() {
ArrayList<User> users = new ArrayList<>();
users.add(User.builder().age(18).name(null).build());
users.add(User.builder().age(null).name("Sakura").build());
ArrayList<Skill> skills = new ArrayList<>();
users.add(User.builder().name("Kuria").age(18).skills(skills).build());
skills.add(Skill.builder().name("py").time(null).build());
skills.add(Skill.builder().name(null).time(LocalDateTime.now()).build());
return users;
}
public static Object replace(Object o, HashMap<Class, HashMap<String,Method>> classMethodMap) {
// 如果对象是集合,递归调用
if (o instanceof Collection)
((Collection<?>) o).forEach(ob -> replace(ob, classMethodMap));
// 如果对象为空返回一个空对象
if (o == null) {
return new Object();
}
Class<?> aClass = o.getClass();
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
try {
// 所有方法缓存
HashMap<String, Method> methodMap = classMethodMap.get(aClass);
if (ObjectUtil.isEmpty(methodMap)) {
// 根据class获取不到缓存,创建新的写入
methodMap = new HashMap<>();
classMethodMap.put(aClass,methodMap);
}
// 获取方法名
String getMethodName = "get" + StrUtil.upperFirst(field.getName());
// 根据方法名获取构造缓存
Method getMethod = methodMap.get(getMethodName);
if (ObjectUtil.isEmpty(getMethod)) {
// 没有缓存,新建缓存
getMethod = aClass.getMethod(getMethodName);
getMethod.setAccessible(true);
// 将方法写入这个class的方法缓存区,方法名作为key
methodMap.put(getMethodName,getMethod);
// 将方法缓存map写入这个class的缓存map缓存区
classMethodMap.put(aClass,methodMap);
}
Object invoke = getMethod.invoke(o);
// 如果获取到的成员属性值是集合,进行递归调用
if (invoke instanceof Collection) {
((Collection<?>) invoke).forEach(ob -> replace(ob, classMethodMap));
}
// 如果属性值不为空直接跳过
if (invoke != null)
continue;
// 缓存部分如上
String setMethodName = "set" + StrUtil.upperFirst(field.getName());
Method setMethod = methodMap.get(aClass.getName() + setMethodName);
if (ObjectUtil.isEmpty(setMethod)) {
setMethod = aClass.getMethod(setMethodName,field.getType());
setMethod.setAccessible(true);
methodMap.put(setMethodName,setMethod);
classMethodMap.put(aClass,methodMap);
}
// 根据字段类型创建新的空对象写入
setMethod.invoke(field.getType().newInstance());
} catch (Exception e) {
System.out.println("出现无法直接使用反射执行的方法" + e.getMessage());
}
}
return o;
}
}
方法二:
在项目中添加配置类
/**
* 处理 jackson 返回的null值
* 返回json中的null值转为空字符串
*/
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
方案三:
使用fastjson替代jackson
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class fastJsonConfig extends WebMvcConfigurationSupport {
/**
* 使用阿里 fastjson 作为 JSON MessageConverter
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(
// 保留 Map 空的字段
SerializerFeature.WriteMapNullValue,
// 将 String 类型的 null 转成""
SerializerFeature.WriteNullStringAsEmpty,
// 将 Number 类型的 null 转成 0
SerializerFeature.WriteNullNumberAsZero,
// 将 List 类型的 null 转成 []
SerializerFeature.WriteNullListAsEmpty,
// 将 Boolean 类型的 null 转成 false
SerializerFeature.WriteNullBooleanAsFalse,
// 避免循环引用
SerializerFeature.DisableCircularReferenceDetect);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(Charset.forName("UTF-8"));
List<MediaType> mediaTypeList = new ArrayList<>();
// 解决中文乱码问题,相当于在 Controller 上的 @RequestMapping 中加了个属性 produces = "application/json"
mediaTypeList.add(MediaType.APPLICATION_JSON);
converter.setSupportedMediaTypes(mediaTypeList);
converters.add(converter);
}
}
坑:
如果配置了这个导致拦截器失效,需要将添加拦截器的配置添加到当前的配置类中,重写同名方法
一般拦截器继承这几个类添加WebMvcConfigurationSupport
; WebMvcConfiguration
,
我之前的拦截器配置在WebMvcConfiguration
中,这里我将这段配置转换器的配置通过重写WebMvcConfiguration
在里面配置发现无法生效,原来这里配置mvc相关的配置以后需要使用@EnableWebMvc
启用
如果在WebMvcConfigurationSupport
中配置拦截器以及转换器就不用这个注解
但是需要注意一点…继承WebMvcConfigurationSupport
的配置类只能有一个,如果使用了这个要把拦截器额配置和同意返回的配置放在一起,否则可能导致其中一个配置无效
使用
后续补充:
慎用方法三,继承WebMvcConfigurationSupport
或者使用@EnableWebMvc
会接管springboot的mvc自动配置,导致自动配置失效
解决
最后加入的时候放入前面就可以了,这样就可以不添加@EnableWebMvc
启用
坑
使用方法三导致默认的ModelAndView
用法失效
public ModelAndView getPage() {
return new ModelAndView("redirect:http://www.baidu.com");
}
异常:
javax.servlet.ServletException: Could not resolve view with name 'redirect:http://www.baidu.com' in servlet with name 'dispatcherServlet'
解决:
添加Bean
@Bean
public InternalResourceViewResolver viewResolver(){
return new InternalResourceViewResolver();
}