SpringBoot如何使用Fastjson解析Json數(shù)據(jù)
方法一:
1.在pom.xml文件下添加依賴(lài)包
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency>
2.修改啟動(dòng)文件
package myshop;
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 App extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// TODO Auto-generated method stub
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastConfig = new FastJsonConfig();
fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastConfig);
converters.add(fastConverter);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App.class, args);
}
}
3.修改實(shí)體類(lèi)
package myshop.entity;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
/**
* 用戶(hù)類(lèi)
*
*/
public class User {
private int id;
private String username;
private String password;
@JSONField(format = "yyyy-MM-dd HH-mm")
private Date createTime;
/**
* 如果不希望返回remark信息
* serialize是否序列化
*/
@JSONField(serialize = false)
private String remark;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
4.修改控制器
package myshop.controller;
import java.util.Date;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import myshop.entity.User;
/**
* @RestController = @Controller + @RequestBody
*
*/
@RestController
public class HelloController {
/**
* 建立請(qǐng)求映射
*
*/
@RequestMapping("/hello")
public String hello() {
return "hello";
}
/**
* SpringBoot默認(rèn)的解析框架Jackson
*
*/
@RequestMapping("/getUser")
public User gerUser()
{
User user = new User();
user.setId(1);
user.setUsername("天恒");
user.setPassword("123456");
user.setCreateTime(new Date());
//此信息不會(huì)被返回
user.setRemark("這是備注信息!");
return user;
}
}
5.啟動(dòng)項(xiàng)目,在瀏覽器輸入地址:http://localhost:8080/getUser

方法二:除了啟動(dòng)類(lèi),其余代碼都和方法一一樣
package myshop;
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 App {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverter()
{
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastConfig = new FastJsonConfig();
fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastConfig);
HttpMessageConverter<?> converts = fastConverter;
return new HttpMessageConverters(converts);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App.class, args);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot中使用FastJson解決long類(lèi)型在js中失去精度的問(wèn)題
- SpringBoot整合Gson 整合Fastjson的實(shí)例詳解
- springboot中用fastjson處理返回值為null的屬性值
- 使用SpringBoot+OkHttp+fastjson實(shí)現(xiàn)Github的OAuth第三方登錄
- SpringBoot整合FastJson過(guò)程解析
- SpringBoot Redis配置Fastjson進(jìn)行序列化和反序列化實(shí)現(xiàn)
- springboot實(shí)現(xiàn)FastJson解析json數(shù)據(jù)的方法
- Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
- Spring?boot詳解fastjson過(guò)濾字段為null值如何解決
相關(guān)文章
Java基礎(chǔ)之final關(guān)鍵字作用案例
這篇文章主要介紹了Java基礎(chǔ)之final關(guān)鍵字作用案例,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
Java 中責(zé)任鏈模式實(shí)現(xiàn)的三種方式
本文重點(diǎn)給大家介紹java中如何編寫(xiě)責(zé)任鏈模式。主要從下面3個(gè)框架中的代碼中介紹。非常不錯(cuò),需要的朋友參考下吧2017-09-09
Java泛型extends關(guān)鍵字設(shè)置邊界的實(shí)現(xiàn)
這篇文章主要介紹了Java泛型extends關(guān)鍵字設(shè)置邊界的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
帶有@Transactional和@Async的循環(huán)依賴(lài)問(wèn)題的解決
這篇文章主要介紹了帶有@Transactional和@Async的循環(huán)依賴(lài)問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Java基礎(chǔ) Servlet監(jiān)聽(tīng)器詳解
這篇文章主要介紹了Java基礎(chǔ) Servlet監(jiān)聽(tīng)器詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
基于Bigdecimal科學(xué)計(jì)數(shù)問(wèn)題
這篇文章主要介紹了基于Bigdecimal科學(xué)計(jì)數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
解決SpringBoot掃描不到公共類(lèi)的實(shí)體問(wèn)題
這篇文章主要介紹了解決SpringBoot掃描不到公共類(lèi)的實(shí)體問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringMVC+Mybatis二維碼實(shí)現(xiàn)多平臺(tái)付款(附源碼)
本文主要實(shí)現(xiàn)微信支付寶等支付平臺(tái)合多為一的二維碼支付,并且實(shí)現(xiàn)有效時(shí)間內(nèi)支付有效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

