spring boot使用fastjson作为HttpMessageConverter

为了统一转换json报文中的null字段为空字符串,集成了fastjson进行统一转换。

一、gradle引入jar

dependencies {
    compile('org.springframework.boot:spring-boot-starter-undertow')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-starter-sleuth')
    compile('org.springframework.boot:spring-boot-starter-data-redis')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('com.github.pagehelper:pagehelper:4.1.0')
    compile('com.alibaba:druid:1.0.18')    
    compile('com.alibaba:fastjson:1.2.38')
    compile files('extlib/ojdbc6.jar')
    compile files('extlib/mybatis-3.4.4.jar')
    compile files('extlib/mybatis-spring-1.3.1.jar')
    compile files('extlib/mybatis-spring-boot-autoconfigure-1.3.0.jar')
    compile files('extlib/mybatis-spring-boot-starter-1.3.0.jar')
    compile files('extlib/jsqlparser-0.9.4.jar')
    runtime('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

、写个配置类

import java.nio.charset.Charset;

import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@Configuration
public class MvcConfig {

	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();

		fastJsonConfig.setSerializerFeatures(
				// 输出空置字段
				SerializerFeature.WriteMapNullValue,
				// list字段如果为null,输出为[],而不是null
				SerializerFeature.WriteNullListAsEmpty,
				// 数值字段如果为null,输出为0,而不是null
				SerializerFeature.WriteNullNumberAsZero,
				// Boolean字段如果为null,输出为false,而不是null
				SerializerFeature.WriteNullBooleanAsFalse,
				// 字符类型字段如果为null,输出为"",而不是null
				SerializerFeature.WriteNullStringAsEmpty);

		fastJsonConfig.setCharset(Charset.forName("UTF-8"));
		fastJsonConfig.setDateFormat("yyyy-MM-dd hh:mm:ss");
		fastConverter.setFastJsonConfig(fastJsonConfig);
		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);
	}
}

、配置文件

在application.yml配置如下:

server:
  port: ${PORT:8083}
  context-path: /testdemo

eureka:
  instance:
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
   
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/**/*.xml
  
spring:
  http:
    encoding:
      force: true
    converters:
      preferred-json-mapper: fastjson

 

转载于:https://my.oschina.net/u/3237413/blog/1544442

要在Spring Boot中整合Fastjson需要进行以下步骤: 1.添加Fastjson依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency> ``` 2.配置FastjsonHttpMessageConverterSpring Boot中我们可以使用`@Configuration`和`@Bean`注解来配置`FastjsonHttpMessageConverter`,并将其添加到`HttpMessageConverters`中。 ```java @Configuration public class FastjsonConfig { @Bean public HttpMessageConverters fastjsonHttpMessageConverter() { //定义一个converters转换消息的对象 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //添加fastjson的配置信息,比如:是否要格式化返回的json数据 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //在converter中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } } ``` 3.测试 在Controller中使用`@RestController`注解,并返回一个Object对象。Fastjson会自动将其转换为JSON格式。 ```java @RestController public class UserController { @GetMapping("/user") public Object getUser() { User user = new User(); user.setId(1L); user.setUsername("test"); user.setPassword("123456"); return user; } } ``` 访问http://localhost:8080/user,可以看到返回的JSON格式数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值