/** * 文件名:@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. Aerospike系列:5:安装AMC

    1:需要安装的包,如果缺少,请安装. python (2.6+) gcc python-devel 安装相应的模块 sudo pip install markupsafe sudo pip insta ...

  2. Spring解决Hibernate中的懒加载问题

    OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中,它将自动被 Spring 的事务管理器探测到. <filter>       ...

  3. Knockoutjs之observable和applyBindings的使用

    observable在Knockoutjs中属于一个核心功能,在做监控数据的改变时,必须要用到Knockoutjs的监控属性——observable. ko.observable()的简单使用 首先来 ...

  4. spring boot mybatis没有扫描jar中的Mapper接口

    只需要在spring boot启动类上加上注解,并指定jar包中接口文件包路径即可 如下: @ComponentScan(basePackages = "com.xx") @Map ...

  5. 用C读取json文件

    a jconf_t * read_jconf(const char *file) { static jconf_t conf; // 清空数据 memset(&conf, , sizeof(j ...

  6. 子类化QTreeWidgetItem实现增加Item的属性

    因为有需求是点击QTreeWidgetItem需要获取该Item的节点的相关属性,Item需要保存关联的属性,那么就需要扩展QTreeWidgetItem,当然,C++中扩展修改一个类或组件的方式就是 ...

  7. 对Java的常用对象(POJO、DTO、PO、BO、VO、DAO)详细解释及应用场景

    首先这些对象都应用都是一些单词的简称,也是一种应用思想,故其他语言也可以使用,在Java里比较常见这些对象吧.下面来一一解释. 一.POJO(Plain Ordinary Java Object). ...

  8. 【RS】List-wise learning to rank with matrix factorization for collaborative filtering - 结合列表启发排序和矩阵分解的协同过滤

    [论文标题]List-wise learning to rank with matrix factorization for collaborative filtering   (RecSys '10 ...

  9. C# 用timer做成服务后 timer_Tick () 为什么不执行?

    不能使用 窗体的 Timer,他只能在窗体中使用,服务中无法使用请使用 System.Timers.Timer类 protected override void OnStart(string[] ar ...

  10. HSSFWorkbook 与 XSSFWorkbook

    刚开始使用new HSSFWorkbook(new FileInputStream(excelFile))来读取Workbook,对Excel2003以前(包括2003)的版本没有问题,但读取Exce ...