每一年都奔走在自己热爱里

没有人是一座孤岛,总有谁爱着你

springboot mybatis获取字段为null时候返回前端不显示

最近nacos微服务项目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);
    }

    @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/");
    }

}
posted @ 2022-06-17 09:54  星星之草%  阅读(1051)  评论(1编辑  收藏  举报