/** * 文件名:@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 ""的更多相关文章

  1. EasyUI - 一般处理程序 返回 Json值

    使用 一般处理程序(ashx)返回Json值. 原始Json数组的格式: [ { ", "name":"张三", "sex":&q ...

  2. springmvc 1.接受日期类型的参数 2.后台返回json串的格式处理(返回json串null值处理为"")

    springmvc中的配置: <bean id="dateConvert" class="com.iomp.util.DateConvert"/> ...

  3. Mysql 返回JSON值属性的函数 (五)

    本节中的函数返回JSON值的属性. JSON_DEPTH(json_doc) 返回JSON文档的最大深度.NULL如果参数为,则 返回 NULL.如果参数不是有效的JSON文档,则会发生错误. 一个空 ...

  4. ajax后台处理返回json值

    public ActionForward xsearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, Htt ...

  5. 关于springmvc 返回json数据null字段的显示问题-转https://blog.csdn.net/qq_23911069/article/details/62063450

    最近做项目(ssm框架)的时候,发现从后台返回的json(fastjson)数据对应不上实体类,从数据库查询的数据,如果对应的实体类的字段没有信息的话,json数据里面就不显示,这不是我想要的结果,准 ...

  6. springboot(五)使用FastJson返回Json视图

    FastJson简介: fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件.它是目前json解析最快的开源组件没有之一!在这之前jaskJs ...

  7. SpringBoot项目中处理返回json的null值

    在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将null值转换为空字符串? 以SpringBoot项目为例,SS ...

  8. springboot+mybatis实现登录功能,返回json

    1.新建maven项目(pom) <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  9. Jquery表单提交后获取返回Json值

    1.给form添加id值: <form action="/News/SaveMessage" method="post" accept-charset=& ...

随机推荐

  1. TQ2440开发板挂载U盘出现乱码

    解决方法:配置内核 make menuconfig File Systems --->      DOS/FAT/NT Filesystems  --->         (utf8) D ...

  2. 神器phpstorm功能具体解释

    phpstorm包括了webstorm的所有功能,更可以支持php代码. PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提供用户效率,可深刻理解用户的编码,提供智能代码补全,高速导航以及即 ...

  3. logrotate日志不轮转呢?_新日志size0

    程序,要管理log,留作日后的后续功能处理和统计和领导需要.因为懒得写,所以直接用了syslog函数,把log交给rsyslog去写了.然后用logrotate每天做日志轮转. 两种log分别发送到了 ...

  4. MySQL优化系列

    https://blog.csdn.net/Jack__Frost/article/details/73347688

  5. Software development --daily scrum team

    History[edit] Scrum was first defined as "a flexible, holistic product development strategy whe ...

  6. Knockout JS 增加、去除、修改绑定

    Knockuot js 似乎只考虑过怎么绑定(ko.applyBindings()),却没考虑过怎么去除绑定,当修改了DOM内容,需要重新绑定时,发现似乎无能为力. 一.解决办法 这里有一个重新绑定的 ...

  7. Solidworks机构运动仿真

    使用Solidworks Motion插件可以对机构进行运动学/动力学仿真.机构约束添加完成后,可以在主动部件(关节)处添加马达进行驱动.运动可以是简单的匀速运动或者复杂的表达式.数据点形式的运动. ...

  8. Windows下VS2017编译OpenCV 3.4.0-rc

    简述 很久没有用过OpenCV了,这次需要做一点图像处理相关的工作,又需要用起来,这里记录一下编译的过程.之前介绍过使用vs2015编译opencv2.4的帖子在这里. 编译好的文件在这里https: ...

  9. java的BASE64Encoder,BASE64Decoder加密与解密

    package com.app.common; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.Fi ...

  10. 【Mysql】php执行脚本进行mysql数据库 备份和还原

    一.mysql备份 1.这里使用 php脚本的形式进行mysql 数据库的备份和还原,想看linux的sh版本的,有时间再贴. 2.找到 mysql的[mysqldump] 执行程序,建议phpinf ...