3、SpringBoot 返回JSON資料格式
阿新 • • 發佈:2018-12-02
SpringBoot
返回 JSON
資料格式
方式一:使用自帶的 jackson
將 Controller
層的 @Controller
註解替換成 @RestController
即可
方式二:完美使用 FastJson
pom
引入依賴:
<!-- 引入fastjson依賴 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</ artifactId>
<version>1.2.49</version>
</dependency>
第一種方法(淘汰了)
- 啟動類繼承
:extends WebMvcConfigurerAdapter
- 覆蓋方法
configureMessageConverters
:
@SpringBootApplication
public class SpringbootJsonApplication extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
/*
* 1、需要先定義一個 convert 轉換訊息的物件;
* 2、新增fastJson 的配置資訊,比如:是否要格式化返回的json資料;
* 3、在convert中新增配置資訊.
* 4、將convert新增到converters當中.
*
*/
// 1、需要先定義一個 convert 轉換訊息的物件;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、新增fastJson 的配置資訊,比如:是否要格式化返回的json資料;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
//3.處理中文亂碼問題
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//4、在convert中新增配置資訊.
fastConverter.setFastJsonConfig(fastJsonConfig);
//5、將convert新增到converters當中.
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootJsonApplication.class, args);
}
}
User
:
package cn.ylx.pojo;
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;
public class User {
private int id;
private String name;
//com.alibaba.fastjson.annotation.JSONField
@JSONField(format="yyyy-MM-dd HH:mm")
private Date createTime;//建立時間.
/*
* 我們不想返回remarks?
* serialize:是否需要序列化屬性.
*/
@JSONField(serialize=false)
private String remarks;//備註資訊.
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = 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;
}
}
HelloController
:
@RestController
public class HelloController {
/**
* Spring Boot預設使用的json解析框架是jackson
* @return
*/
@RequestMapping("/getUser")
public User getUser(){
User user = new User();
user.setId(1);
user.setName("哈哈");
user.setCreateTime(new Date());
user.setRemarks("標記資訊");
return user;
}
}
第二種方法(推薦)
- 啟動項注入Bean:HttpMessageConverters
@Bean注入第三方的json解析框架:
啟動項:
package cn.ylx;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class SpringbootJsonApplication {
/**
* 在這裡我們使用 @Bean注入 fastJsonHttpMessageConvert
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1、需要先定義一個 convert 轉換訊息的物件;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、新增fastJson 的配置資訊,比如:是否要格式化返回的json資料;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.處理中文亂碼問題
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//3、在convert中新增配置資訊.
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootJsonApplication.class, args);
}
}