最近springBoot项目mybatis获取字段为null时候返回前端不显示,这不是我想要的结果。我想要结果无论null还是空字段串都给前端返回字段。
@Configuration
@EnableWebMvc
public class GlobalResourceConfig implements WebMvcConfigurer {
//两种方法参考
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setCharset(StandardCharsets.UTF_8);
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue);//主要看这行代码
//处理日期时间格式化问题
fastJsonConfig.setDateFormat("yyyy-MM-dd hh:mm:ss");
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON);
//在convert中添加配置信息.
fastConverter.setSupportedMediaTypes(fastMediaTypes);
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
converters.add(stringHttpMessageConverter);
fastConverter.setFastJsonConfig(fastJsonConfig);
//将convert添加到converters中
converters.add(fastConverter);
}
//两种方法参考
/**
* 使用阿里 FastJson 作为JSON MessageConverter
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
//保留空的字段
config.setCharset(StandardCharsets.UTF_8);
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat,SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue);
//SerializerFeature.PrettyFormat 这是格式化返回前端
//SerializerFeature.WriteMapNullValue 这是mybatis-plus 查询到以后不管有没有值都返回给前端
// SerializerFeature.WriteNullStringAsEmpty,//String null -> "" 这是String类型如果为null则返回""
// SerializerFeature.WriteNullNumberAsZero//Number null -> 0这是Number类型如果为null则返回0
// 格式化输出
//SerializerFeature.PrettyFormat,
// 消除循环引用
//SerializerFeature.DisableCircularReferenceDetect,
// 返回结果保留null值
//SerializerFeature.WriteMapNullValue,
// 将返回值为null的字符串转变成"",在这里可以自己设置
//SerializerFeature.WriteNullStringAsEmpty,
// List字段如果为null,输出为[],而非null
//SerializerFeature.WriteNullListAsEmpty
// 按需配置,更多参考FastJson文档哈
MediaType[] mediaTypes = new MediaType[]{
MediaType.APPLICATION_OCTET_STREAM,
MediaType.APPLICATION_JSON,
MediaType.TEXT_HTML,
MediaType.TEXT_PLAIN,
MediaType.TEXT_XML,
MediaType.APPLICATION_STREAM_JSON,
MediaType.APPLICATION_ATOM_XML,
MediaType.APPLICATION_FORM_URLENCODED,
MediaType.APPLICATION_PDF,
};
converter.setSupportedMediaTypes(Arrays.asList(mediaTypes));
converter.setFastJsonConfig(config);
converter.setDefaultCharset(StandardCharsets.UTF_8);
// converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
converters.add(new ByteArrayHttpMessageConverter());
converters.add(converter);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/script/**").addResourceLocations("classpath:/script/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
标签:MediaType,SerializerFeature,空值,APPLICATION,plus,Mybatis,new,null,converters
From: https://www.cnblogs.com/wjsqqj/p/16585892.html