本人使用FastJson比较熟悉,SpringBoot使用JSON不习惯,需要替换成FastJson。
通过观看https://my.oschina.net/sdlvzg/blog/1154281创建项目,再执行以下操作
引入依赖库:
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<!-- 改变spring boot 默认的json解析,通过引入fastjson改变 -->
<!-- fastjson版本必须 大于1.2.10 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.23</version>
</dependency>
配置FastJson
有两种方法:
方法一:在App.java启动类中,注入Bean : HttpMessageConverters
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
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;
/**
* Spring Boot启动类
*
* @author Administrator
*
*/
@SpringBootApplication // 等价于@Configuration,@EnableAutoConfiguration和@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
//引入Fastjson解析json,不使用默认的jackson
//必须在pom.xml引入fastjson的jar包,并且版必须大于1.2.10
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
//1、定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters中
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
方法二:启动类继承extends WebMvcConfigurerAdapter,覆盖方法configureMessageConverters
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
* Spring Boot启动类
*
* @author Administrator
*
*/
@SpringBootApplication // 等价于@Configuration,@EnableAutoConfiguration和@ComponentScan
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
// 引入Fastjson解析json,不使用默认的jackson
// 必须在pom.xml引入fastjson的jar包,并且版必须大于1.2.10
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1、定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters中
converters.add(fastConverter);
}
}
验证是否生效
创建实体:
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
public class Demo {
private long id;// 主键.
private String name;// 测试名称.
@JSONField(serialize=false) //转为JSON时不包括此属性
private String remark; //备注
@JSONField(format="yyyy-MM-dd HH:mm") //转为JSON时按格式转换
private Date createTime;
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
创建Controller
import java.util.Date;
import org.lvgang.bean.Demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
/**
* 返回demo数据:
* 请求地址:http://127.0.0.1:8080/demo/getDemo
* @return
*/
@RequestMapping("/getDemo")
public Demo getDemo(){
Demo demo = new Demo();
demo.setId(1);
demo.setName("Angel");
demo.setCreateTime(new Date());
demo.setRemark("remark");
return demo;
}
}
通过前台访问http://127.0.0.1:8080/demo/getDemo,返回的结果为:
{ "createTime":"2017-07-04 10:07", "id":1, "name":"Angel" } 表示配置完成