[TOC]html
隨着web開發先後端分離技術的盛行,json是目前主流的先後端數據交互方式,使用json數據進行交互須要對json數據進行轉換解析,須要用到一些json處理器,經常使用的json處理器有:java
下面分別介紹如何在SpringBoot中整合三大json解析框架。web
Jackson-databind是SpringBoot默認集成在web依賴中的框架,所以咱們只須要引入spring-boot-starter-web
依賴,就能夠返回json數據:spring
接着上篇文章中的demo繼續修改demo,先看下代碼框架:json
下面開始修改demo,返回json數據,首先在pojo下建立一個Good實體類,而且能夠經過註解來解決日期格式等需求:後端
package com.gongsir.springboot02.pojo; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.Date; public class Good { private Integer id; private String name; @JsonIgnore private Double price; @JsonFormat(pattern = "yy-MM-dd") private Date dealDate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Date getDealDate() { return dealDate; } public void setDealDate(Date dealDate) { this.dealDate = dealDate; } }
而後在controller包下建立一個GoodController,在方法上加上@ResponseBody
註解(也能夠直接使用@RestController
註解整個類)便可返回json信息:瀏覽器
@Controller @RequestMapping(value = "/good") public class GoodController { @GetMapping(path = "/get") @ResponseBody public Good getGood(){ Good good = new Good(); good.setId(1); good.setName("MacBook Pro 2019"); good.setPrice(16999.99); good.setDealDate(new Date()); return good; } }
此時可使用瀏覽器或者postman工具來查看結果,運行項目,調用http://localhost:8080/good/get接口:springboot
使用Gson須要將SpringBoot默認依賴的jackson-databind除去,而後引入Gson:app
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--除去jackson-databind依賴--> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </exclusion> </exclusions> </dependency> <!--引入Gson依賴--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>
建立一個gson配置:框架
package com.gongsir.springboot02.configuration; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.GsonHttpMessageConverter; import java.lang.reflect.Modifier; @Configuration public class GsonConfig { @Bean GsonHttpMessageConverter gsonHttpMessageConverter(){ GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter(); GsonBuilder builder = new GsonBuilder(); //設置解析的日期格式 builder.setDateFormat("yyyy-MM-dd"); //設置忽略字段,忽略修飾符爲protected的字段屬性 builder.excludeFieldsWithModifiers(Modifier.PROTECTED); Gson gson = builder.create(); gsonHttpMessageConverter.setGson(gson); return gsonHttpMessageConverter; } }
修改Good,java:
package com.gongsir.springboot02.pojo; import java.util.Date; public class Good { protected Integer id; private String name; private Double price; private Date dealDate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Date getDealDate() { return dealDate; } public void setDealDate(Date dealDate) { this.dealDate = dealDate; } }
啓動項目,再次訪問http://localhost:8080/good/get接口:
引入fastjson依賴,修改剛剛的pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--除去jackson-databind依賴--> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </exclusion> </exclusions> </dependency> <!--引入fastjson依賴--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
配置fastjson有兩種方法,方法一:自定義MyFastjsonConfig,提供FastJsonHttpMessageConverter Bean:
package com.gongsir.springboot02.configuration; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @Configuration public class MyFastjsonConfig { @Bean FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){ FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); //升級以後的fastjson(1.2.28以後的版本)須要手動配置MediaType,不然會報錯 List<MediaType> supportedMediaTypes = new ArrayList<>(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML); supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); supportedMediaTypes.add(MediaType.APPLICATION_PDF); supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML); supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML); supportedMediaTypes.add(MediaType.APPLICATION_XML); supportedMediaTypes.add(MediaType.IMAGE_GIF); supportedMediaTypes.add(MediaType.IMAGE_JPEG); supportedMediaTypes.add(MediaType.IMAGE_PNG); supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM); supportedMediaTypes.add(MediaType.TEXT_HTML); supportedMediaTypes.add(MediaType.TEXT_MARKDOWN); supportedMediaTypes.add(MediaType.TEXT_PLAIN); supportedMediaTypes.add(MediaType.TEXT_XML); converter.setSupportedMediaTypes(supportedMediaTypes); config.setDateFormat("yyyy-MM-dd"); config.setCharset(Charset.forName("UTF-8")); config.setSerializerFeatures( //json格式化 SerializerFeature.PrettyFormat, //輸出value爲null的數據 SerializerFeature.WriteMapNullValue ); converter.setFastJsonConfig(config); return converter; } }
方法二:實現WebMvcConfigurer接口,重寫configureMessageConverters方法:
package com.gongsir.springboot02.configuration; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @Configuration public class FastJsonConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); com.alibaba.fastjson.support.config.FastJsonConfig config = new com.alibaba.fastjson.support.config.FastJsonConfig(); //升級以後的fastjson(1.2.28以後的版本)須要手動配置MediaType,不然會報錯 List<MediaType> supportedMediaTypes = new ArrayList<>(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML); supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); supportedMediaTypes.add(MediaType.APPLICATION_PDF); supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML); supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML); supportedMediaTypes.add(MediaType.APPLICATION_XML); supportedMediaTypes.add(MediaType.IMAGE_GIF); supportedMediaTypes.add(MediaType.IMAGE_JPEG); supportedMediaTypes.add(MediaType.IMAGE_PNG); supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM); supportedMediaTypes.add(MediaType.TEXT_HTML); supportedMediaTypes.add(MediaType.TEXT_MARKDOWN); supportedMediaTypes.add(MediaType.TEXT_PLAIN); supportedMediaTypes.add(MediaType.TEXT_XML); converter.setSupportedMediaTypes(supportedMediaTypes); config.setDateFormat("yyyy-MM-dd"); config.setCharset(Charset.forName("UTF-8")); config.setSerializerFeatures( //json格式化 SerializerFeature.PrettyFormat, //輸出value爲null的數據 SerializerFeature.WriteMapNullValue ); converter.setFastJsonConfig(config); //將converter加入到converters converters.add(converter); } }
啓動項目,再次調用接口:
原文出處:https://www.cnblogs.com/gongsir/p/11654013.html