Spring Boot完美使用FastJson解析JSON数据
2017-06-24 本文已影响689人
任重而道元
需要引入Fastjson的依赖
<!-- 添加fastjson依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
第一种方案:
DemoApplication继承WebMvcConfigurerAdapter,重写configureMessageConverters方法。
package com.example.demo;
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;
@SpringBootApplication
public class DemoApplication extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
// 1.需要先定义一个vonvert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如:是否格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在conver中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将vonver添加到converters当中
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
添加测试实体类Demo.java
package com.example.demo;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
/**
* 这是一个测试实体
* @author Lidy
*/
public class Demo {
private int id;
private String name;
@JSONField(format = "yyyy-MM-dd HH:ss:mm")
private Date createTime;//创建时间
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
编写Controller
package com.example.demo;
import java.util.Date;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 最简单的控制类
* @author Lidy
*/
@RestController
public class HelloController {
/**
* Spring Boto默认使用的的json解析框架是jackson
* 自动将对象解析成json数据
* @return
*/
@RequestMapping("/getDemo")
public Demo getDemo(){
Demo demo = new Demo();
demo.setId(1);
demo.setName("道哥");
demo.setCreateTime(new Date());
return demo;
}
}
编写浏览器查看返回json数据,是否是在Demo.java中规定的格式
Paste_Image.png第二种方案:
@Bean注入第三方的json解析框架。
package com.example.demo;
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;
@SpringBootApplication
public class DemoApplication {
/**
* 在这里我们使用@Bean注入fastJsonHttpMessageConverters
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
// 1.需要先定义一个vonvert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如:是否格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在conver中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
// 4.将vonver添加到converters当中
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
不想返回Demo.java中的字段,可以下面这样添加
/**
* 我们不想返回remarks
* deserialize:是否需要序列化属性
*/
@JSONField(serialize = false)
private String remarks;//备注 信息