一:添加fastjson依赖

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.22</version>
</dependency>

  

三:创建配置文件

在文件夹configurer中创建WebConfigurer

package com.example.demo.core.configurer;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
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; /**
* @author
* @Description:
* @time 2018/4/19 10:42
*/
@Configuration
public class WebConfigurer extends WebMvcConfigurationSupport { /**
* 修改自定义消息转换器
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
converter.setSupportedMediaTypes(getSupportedMediaTypes());
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(
// String null -> ""
SerializerFeature.WriteNullStringAsEmpty,
// Number null -> 0
SerializerFeature.WriteNullNumberAsZero,
//禁止循环引用
SerializerFeature.DisableCircularReferenceDetect
);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(Charset.forName("UTF-8"));
converters.add(converter);
} private List<MediaType> getSupportedMediaTypes() {
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);
return supportedMediaTypes;
}
}

  其中config.setSerializerFeatures()方法中可以添加多个配置,以下列举出几个常用配置

  WriteNullListAsEmpty :List字段如果为null,输出为[],而非null
  WriteNullStringAsEmpty : 字符类型字段如果为null,输出为"",而非null
  DisableCircularReferenceDetect :消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
  WriteNullBooleanAsFalse:Boolean字段如果为null,输出为false,而非null
  WriteMapNullValue:是否输出值为null的字段,默认为false

五:测试

{
"code": 200,
"data": {
"id": 2,
"userName": "" //这里已经变为"",而不是null
},
"msg": "success"
}

  

  

(四)SpringBoot如何定义消息转换器的更多相关文章

  1. springmvc-自定义消息转换器

    最近的项目没有用到这个,先把自己自学跑通的例子先帖出来,供自己以后参考吧! 如有不对地方望指出! 一.自定义类实现AbstractHttpMessageConverter package com.dz ...

  2. 【spring-boot神器】第一篇:拦截器,过滤器,监听器,控制器,消息转换器,AOP执行顺序

    整理一下这几天学习的资料和代码 第一部分.上代码 1.spring各种器的实现,idea搭建spring-boot的教程在这里http://www.jianshu.com/p/9082a533fa3c ...

  3. SpringBoot 消息转换器 HttpMessageConverter

    1.简介: Spring在处理请求时,由合适的消息转换器将请求报文绑定为方法中的形参对象,在这里,同一个对象就有可能出现多种不同的消息形式,比如json和xml.同样,当响应请求时,方法的返回值也同样 ...

  4. springboot核心技术(五)-----消息(rabbitmq)

    消息 1. 大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力 2. 消息服务中两个重要概念: 消息代理(message broker)和目的地(destination) 当消息发送者发 ...

  5. HandlerMethodArgumentResolver(三):基于消息转换器的参数处理器【享学Spring MVC】

    每篇一句 一个事实是:对于大多数技术,了解只需要一天,简单搞起来只需要一周.入门可能只需要一个月 前言 通过 前面两篇文章 的介绍,相信你对HandlerMethodArgumentResolver了 ...

  6. C#微信公众号开发系列教程四(接收普通消息)

    微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南) C#微信公众号开发系列教程三(消息体签名及加解密) C ...

  7. SpringMVC自定义配置消息转换器踩坑总结

    问题描述 最近在开发时候碰到一个问题,springmvc页面向后台传数据的时候,通常我是这样处理的,在前台把数据打成一个json,在后台接口中使用@requestbody定义一个对象来接收,但是这次数 ...

  8. SpringMVC——消息转换器HttpMessageConverter(转)

    文章转自http://blog.csdn.net/cq1982/article/details/44101293 概述 在SpringMVC中,可以使用@RequestBody和@ResponseBo ...

  9. ActiveMQ之spring集成消息转换器MessageConverter

    MessageConverter的作用主要有两方面,一方面它可以把我们的非标准化Message对象转换成我们的目标Message对象,这主要是用在发送消息的时候:另一方面它又可以把我们的Message ...

随机推荐

  1. FragmentSharedFabTransition

    https://github.com/lgvalle/FragmentSharedFabTransition

  2. POJ 2524 Ubiquitous Religions (幷查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23090   Accepted:  ...

  3. 【Web前端】清除浮动&amp;css中文字体

    清理浮动有非常多种方式,像使用 br 标签自带的 clear 属,使用元素的 overflow.使用空标签来设置 clear:both 等等.但考虑到兼容问题和语义化的问题,一般我们都会使用例如以下代 ...

  4. EF中 Code-First 方式的数据库迁移

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...

  5. Spark学习笔记:(一)入门 glance

    参考: http://spark.apache.org/docs/latest/quick-start.html 其它资料:    http://mojijs.com/2015/04/190845/i ...

  6. IE8与vs2005冲突 添加MFC类向导错误解决方法—— internet explorer脚本错误

    IE8 与 VS2005 冲突问题解决方法 问题表现为: MFC类向导添加类时,出现“当前页面的脚本发生错误”,进入MFC类向导后上方有一个小黄条“此网站的某个加载项运行失败.请检查"Int ...

  7. Flume-ng-sdk源码分析

    Flume 实战(2)--Flume-ng-sdk源码分析 - mumuxinfei - 博客园 http://www.cnblogs.com/mumuxinfei/p/3823266.html

  8. using the flume-ng-sql-source plugin to push data from Mysql DB to Spark

    org.apache.flume.EventDeliveryException while running flume and sending data to spark · Issue #21 · ...

  9. Cluster Mode Overview

    https://spark.apache.org/docs/latest/cluster-overview.html

  10. Yii2 behaviors中verbs access的一些理解

    public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'verbs' => [ 'clas ...