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);}
时间: 2024-08-04 19:55:14

springboot 利用configureMessageConverters add FastJsonHttpMessageConverter 实现返回JSON值 null to ""的相关文章

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

1.给form添加id值: <form action="/News/SaveMessage" method="post" accept-charset="utf-8" class="form" id="frm-reg" name="frm-reg"> 设置所有input标签的name属性值为数据库的字段值,即可以传值到后台 2.给提交按钮添加id值: <input cl

spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500:no convertter for return value of type

问题描述 spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson spring boot 新增配置解决后台返回 json 到前台中文乱码之后,出现返回json数据报错:no convertter for return value of type 注释掉解决中文乱码的问题之后返回对象json正常 Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWr

webapi返回json格式优化

一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 config.Formatters.Remove(config.Formatters.XmlFormatter); 二.设置返回Json键值统一为小写 新建一个类并继承自DefaultContractResolver,重写ResolvePropertyName方法, public class UnderlineSplitContractResolver : Defau

SpringBoot 03_利用FastJson返回Json数据

自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson来实现对数据的json序列化. 在使用fastjson时,可以有以下两种集成方式,但是都需要引入fastjson的依赖包 1:引入fastjson依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId&g

SpringBoot 02_返回json数据

在SpringBoot 01_HelloWorld的基础上来返回json的数据,现在前后端分离的情况下多数都是通过Json来进行交互,下面就来利用SpringBoot返回Json格式的数据. 1:新建Pesron.java @Data public class Person { //编号 private String id; // 姓名 private String name; // 性别 private String gender; } 至于@Data注解的作用,请参考Lombok教程. 2:

springboot返回json和xml

在项目中某些情况下接口需要返回的是xml数据或者json数据 1.在springboot项目加入加入依赖jackson-dataformat-xml <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </dependency> <dependency>

上手spring boot项目(四)之springboot如何返回json数据

在springboot整合thymeleaf中,经常会在HTML页面中接收来自服务器的json数据,然后处理json数据并在页面上渲染.那么如何在服务器中返回json类型的数据呢? 1.使用@ResponseBody注解 该注解用于将 Controller 的方法返回的对象,通过 HttpMessageConverter 接口转换为指定格式的 数据如:json,xml 等,通过 Response 响应给客户端 在controller的方法上增加@RespongBody @RequestMappi

ajax请求后台,返回json格式数据,模板!

添加一个用户的时候,需要找出公司下所有的部门,和相应部门下的角色,利用ajax请求,实现联动技术.将返回的json格式数据,添加到select标签下. <script type="text/javascript">        //加载出部门的信息            function loadGroup(){                            $.ajax({                    type:"post",  

java之后台返回json格式字符串,前台接受并转为json文件

作为一个菜鸟,做项目真的好困难呀,这两天被一个问题困了两天,终于解决了,但是也不算太完美.首先,先说一下问题吧,根据后台返回的值,前台接受并作出一个折线图. 最初,在后台根据从数据库中的值,强制拼成了json格式的字符串,利用model返回给前台,前台Ajax  success:function(data)data接收数据,这是应该接收的是字符串,由于折线图中series中的data为非字符串,(这个是后来百度才知道的),就没有成功.然后就想后台直接将数据转换成json,然后返回前台,利用里JS