Spring boot 返回json數(shù)據(jù)
編寫實(shí)體類Student
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* 這是一個(gè)測(cè)試實(shí)體類
*/
public class Student {
private String id;
private String name;
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthdate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
}
Springboot默認(rèn)使用jackson解析json日期類型序列化格式需要在時(shí)間屬性上加
@JsonFormat(timezone="GMT+8",pattern="yyyy-MM-dd")
是將String轉(zhuǎn)換成Date汁尺,一般前臺(tái)給后臺(tái)傳值時(shí)用
@DateTimeFormat(pattern="yyyy-MM-dd")
是將Date轉(zhuǎn)換成String 一般后臺(tái)傳值給前臺(tái)時(shí)使用
編寫getStudent方法
package com.springboot.backstage.controller;
import com.springboot.backstage.entity.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
//SpringBoot提供了refult風(fēng)格
// @RestController相當(dāng)于@Controller和@ResponseBody
@RestController
public class HellController {
/**
* Springbootm默認(rèn)使用jackson解析json
* @return
*/
@RequestMapping("/getStudent")
public Student getStudent(){
Student student =new Student();
student.setId("1");
student.setName("張三");
student.setBirthdate(new Date());
return student;
}
}
運(yùn)行main函數(shù)測(cè)試
@SpringBootApplication
public class SpringBootApp extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class,args);
}
}
這里推薦一個(gè)google瀏覽器插件(JSON Viewer)可以更清楚的展示json數(shù)據(jù)
Spring boot使用FastJson解析JSON數(shù)據(jù)
引入fastjson依賴庫(kù)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
這里要說(shuō)下很重要的話弟蚀,官方文檔說(shuō)的1.2.10以后子檀,會(huì)有兩個(gè)方法支持HttpMessageconvert曹洽,一個(gè)是FastJsonHttpMessageConverter,支持4.2以下的版本双泪,一個(gè)是FastJsonHttpMessageConverter4支持4.2以上的版本坞古,具體有什么區(qū)別暫時(shí)沒有深入研究。這里也就是說(shuō):低版本的就不支持了酱鸭,所以這里最低要求就是1.2.10+吗垮。
第一種方法
第一種方法就是:
(1)啟動(dòng)類繼承extends WebMvcConfigurerAdapter
(2)覆蓋方法configureMessageConverters
代碼
package com.springboot.backstage.controller;
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.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class SpringBootApp extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1.需要先定義一個(gè)convert消息轉(zhuǎn)換的對(duì)象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2.添加fastJson的配置信息,比如:要格式化返回的json數(shù)據(jù)
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);
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class,args);
}
}
測(cè)試
把Student類的date對(duì)象改成能接收f(shuō)astjson返回的date如果被格式化說(shuō)明已經(jīng)使用fastjson解析
@JSONField(format = "yyyy-MM-dd")
fastjson另一個(gè)參數(shù)可以讓屬性不參與序列化
@JSONField(serialize=false)
package com.springboot.backstage.entity;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* 這是一個(gè)測(cè)試實(shí)體類
*/
public class Student {
private String id;
private String name;
@JSONField(format = "yyyy-MM-dd")
private Date birthdate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
}
第二種方法
在這里使用@Bean注入FastJsonHttpMessageConverter
代碼
package com.springboot.backstage.controller;
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.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class SpringBootApp {
/**
* 在這里使用@Bean注入FastJsonHttpMessageConverter
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverter(){
//1.需要先定義一個(gè)convert消息轉(zhuǎn)換的對(duì)象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//2.添加fastJson的配置信息,比如:要格式化返回的json數(shù)據(jù)
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.處理中文亂碼問題
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//4.在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class,args);
}
}