springboot 利用configureMessageConverters add FastJsonHttpMessageConverter 实现返回JSON值 null to ""
/** * 文件名:@WebConfiguration.java <br/> * @author tomas <br/> import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.actuate.health.ApplicationHealthIndicator;import org.springframework.boot.actuate.health.HealthIndicator;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.context.EnvironmentAware;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import org.springframework.http.MediaType;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.ArrayList;import java.util.List; import static com.alibaba.fastjson.serializer.SerializerFeature.*; /** * 类名:WebConfiguration <br /> * * 功能:Web相关配置 * * @author tomas <br /> * 创建时间:2016年7月27日 下午3:57:19 <br /> * @version 2016年7月27日*/@Configuration@EnableWebMvcpublic class FrontConfiguration extends WebMvcConfigurerAdapter implements EnvironmentAware { // 日志记录器 private static final Logger logger = LoggerFactory.getLogger(FrontConfiguration.class); // 当前的环境对象 protected Environment environment; @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); //1.需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); // 不忽略对象属性中的null值 fastJsonConfig.setSerializerFeatures( PrettyFormat, WriteNullListAsEmpty, WriteNullStringAsEmpty); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); //4、将convert添加到converters当中. converters.add(fastConverter); } public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("favicon.ico") .addResourceLocations("classpath:/static/favicon.ico"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * Set the {@code Environment} that this object runs in. * * @param environment */ @Override public void setEnvironment(Environment environment) { this.environment = environment; }}@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); //1.需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); List<MediaType> supportedMediaTypes = new ArrayList<>(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML); supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); supportedMediaTypes.add(MediaType.APPLICATION_PDF); supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML); supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML); supportedMediaTypes.add(MediaType.APPLICATION_XML); supportedMediaTypes.add(MediaType.IMAGE_GIF); supportedMediaTypes.add(MediaType.IMAGE_JPEG); supportedMediaTypes.add(MediaType.IMAGE_PNG); supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM); supportedMediaTypes.add(MediaType.TEXT_HTML); supportedMediaTypes.add(MediaType.TEXT_MARKDOWN); supportedMediaTypes.add(MediaType.TEXT_PLAIN); supportedMediaTypes.add(MediaType.TEXT_XML); fastConverter.setSupportedMediaTypes(supportedMediaTypes); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); // 不忽略对象属性中的null值 fastJsonConfig.setSerializerFeatures( PrettyFormat, WriteNullListAsEmpty, WriteNullStringAsEmpty); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); //4、将convert添加到converters当中. converters.add(fastConverter);}
springboot 利用configureMessageConverters add FastJsonHttpMessageConverter 实现返回JSON值 null to ""的更多相关文章
- EasyUI - 一般处理程序 返回 Json值
使用 一般处理程序(ashx)返回Json值. 原始Json数组的格式: [ { ", "name":"张三", "sex":&q ...
- springmvc 1.接受日期类型的参数 2.后台返回json串的格式处理(返回json串null值处理为"")
springmvc中的配置: <bean id="dateConvert" class="com.iomp.util.DateConvert"/> ...
- Mysql 返回JSON值属性的函数 (五)
本节中的函数返回JSON值的属性. JSON_DEPTH(json_doc) 返回JSON文档的最大深度.NULL如果参数为,则 返回 NULL.如果参数不是有效的JSON文档,则会发生错误. 一个空 ...
- ajax后台处理返回json值
public ActionForward xsearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, Htt ...
- 关于springmvc 返回json数据null字段的显示问题-转https://blog.csdn.net/qq_23911069/article/details/62063450
最近做项目(ssm框架)的时候,发现从后台返回的json(fastjson)数据对应不上实体类,从数据库查询的数据,如果对应的实体类的字段没有信息的话,json数据里面就不显示,这不是我想要的结果,准 ...
- springboot(五)使用FastJson返回Json视图
FastJson简介: fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件.它是目前json解析最快的开源组件没有之一!在这之前jaskJs ...
- SpringBoot项目中处理返回json的null值
在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将null值转换为空字符串? 以SpringBoot项目为例,SS ...
- springboot+mybatis实现登录功能,返回json
1.新建maven项目(pom) <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- Jquery表单提交后获取返回Json值
1.给form添加id值: <form action="/News/SaveMessage" method="post" accept-charset=& ...
随机推荐
- IDEA Run/Debug Configurations 中点击“+”号没有tomcat server选项
环境: 版本:IntelliJ IDEA 2016.3.2系统:windows7 32位 / ubuntu上通用 2.问题: 在IDEA中,对每一个web项目都要配置tomcat服务器,如果你是第一次 ...
- Communication API
Stingray WIKI Stingray javascript Communication 主要的三个方法: Communication.LinkRequest - 页面跳转,调用比较简单,直接参 ...
- Python学习笔记_03:简单操作MongoDB数据库
目录 1. 插入文档 2. 查询文档 3. 更新文档 4. 删除文档 1. 插入文档 # -*- coding: UTF-8 -*- import datetime from pymongo im ...
- scpclient使用报错fchmod无法找到问题解决
因为这个函数时linux下专用的,Windows下无法使用,所以会导致提示这个函数不能使用,解决的方法如下: 1. import platform 2. if platform.system() == ...
- 来自Unix/Linux的编程启发录
本篇文章已授权微信公众号 guolin_blog (郭霖)独家公布重点内容 2017年第一篇文章,祝各位好友新年快乐. 年前因为不小心坐到了自己左手大拇指导致轻微的骨裂,没有按时更新,实在是羞愧.今年 ...
- python3用http.server模块搭建简易版服务器
基本流程: 1.需要的支持 1)python3用http模块下的子模块,即:http.server模块 2)将希望共享的文件放在c盘下,如:C:\游戏行业面试专用 2.打开cmd,cd c:\\pyt ...
- Spring Cloud Dalston.SR5 BUG一记
使用Dalston.SR5版本的Zuul时, 发现Ribbon重试不能切换服务实例, 换成Edgware.SR3,同样的配置可以切换实例进行重试 还有个不升级所有Spring Cloud组件的方法,仅 ...
- Oracle死锁导致的tomcat抛损坏的管道异常
今天遇到个问题,就是由于清空数据库的数据,设备采集客户端一直在往服务器上发采集回来的数据,但是由于某种原因,数据库的某语句死锁了. 数据库的死锁语句是通过这个SQL查出来的:select sql_te ...
- Maven Package Plugins And Maven Test
在使用Maven时候,大家避免不了使用大量插件: 1. maven-compiler-plugin 指定JDK版本和编码方式 ? ?compiler插件能解决2个问题: 第一:指定JDK版本--mav ...
- spring+mybatis+atomikos 实现JTA事务
1. 选择哪种transaction manager? 在单数据源情况下,JDBC,Hibernate,ibatis等自带的 transaction manager已能用于处理事务. ...