SpringBoot如何返回Json數(shù)據(jù)格式
一、@RestController 注解
在 Spring Boot 中的 Controller 中使用 @RestController 注解即可返回 JSON 格式的數(shù)據(jù)。
@RestController
注解包含了 @Controller 和 @ResponseBody 注解。@ResponseBody
注解是將返回的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為 JSON 格式。
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller @ResponseBody public @interface RestController { String value() default ""; }
二、Jackson
在 Spring Boot 中默認使用的 JSON 解析技術(shù)框架是 Jackson。
點開 pom.xml 中的 spring-boot-starter-web 依賴,可以看到 spring-boot-starter-json 依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> <version>2.0.3.RELEASE</version> <scope>compile</scope> </dependency>
再次點進去上面提到的 spring-boot-starter-json 依賴,可以看到如下代碼:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jdk8</artifactId> <version>2.9.6</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.9.6</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-parameter-names</artifactId> <version>2.9.6</version> <scope>compile</scope> </dependency>
到此為止,可以知道 Spring Boot 中默認使用的 JSON 解析框架是 Jackson。
1、對象、List、Map 轉(zhuǎn)換為Json格式
創(chuàng)建實體類:
public class User { private Long id; private String username; private String password; /* 省略get、set和帶參構(gòu)造方法 */ }
Controller 層
import com.itcodai.course02.entity.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/json") public class JsonController { @RequestMapping("/user") public User getUser() { return new User(1, "倪升武", "123456"); //返回 {"id":1,"username":"倪升武","password":"123456"} } @RequestMapping("/list") public List<User> getUserList() { List<User> userList = new ArrayList<>(); User user1 = new User(1, "倪升武", "123456"); User user2 = new User(2, "達人課", "123456"); userList.add(user1); userList.add(user2); return userList; //返回 [{"id":1,"username":"倪升武","password":"123456"},{"id":2,"username":"達人課","password":"123456"}] } @RequestMapping("/map") public Map<String, Object> getMap() { Map<String, Object> map = new HashMap<>(3); User user = new User(1, "倪升武", "123456"); map.put("作者信息", user); map.put("博客地址", "http://blog.itcodai.com"); map.put("CSDN地址", "http://blog.csdn.net/eson_15"); map.put("粉絲數(shù)量", 4153); return map; //返回 {"作者信息":{"id":1,"username":"倪升武","password":"123456"},"CSDN地址":"http://blog.csdn.net/eson_15","粉絲數(shù)量":4153,"博客地址":"http://blog.itcodai.com"} } }
2、Jackson 的配置類
在轉(zhuǎn) JSON 格式的時候?qū)⑺械?null 轉(zhuǎn)換為 “” 的配置
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import java.io.IOException; @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(""); } }); return objectMapper; } } // 修改一下上面返回 Map 的接口,將幾個值改成 null 測試一下: @RequestMapping("/map") public Map<String, Object> getMap() { Map<String, Object> map = new HashMap<>(3); User user = new User(1, "倪升武", null); map.put("作者信息", user); map.put("博客地址", "http://blog.itcodai.com"); map.put("CSDN地址", null); map.put("粉絲數(shù)量", 4153); return map; // 返回 {"作者信息":{"id":1,"username":"倪升武","password":""},"CSDN地址":"","粉絲數(shù)量":4153,"博客地址":"http://blog.itcodai.com"} // 可以看到 Jackson 已經(jīng)將所有 null 字段轉(zhuǎn)成空字符串了。 }
三、Fastjson
Fastjson 是阿里巴巴開源的。
Jackson 和 fastjson 有哪些區(qū)別?
從擴展上來看,fastjson 沒有 Jackson 靈活,從速度或者上手難度來看,fastjson 可以考慮,它也比較方便。
fastjson 的依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.35</version> </dependency>
Fastjson 配置類
使用 fastjson 時,對 null 的處理和 Jackson 有些不同,需要繼承 WebMvcConfigurationSupport 類,然后覆蓋 configureMessageConverters 方法。
在方法中,我們可以選擇要實現(xiàn) null 轉(zhuǎn)換的場景,代碼如下:
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.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @Configuration public class fastJsonConfig extends WebMvcConfigurationSupport { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setDateFormat("yyyy-MM-dd"); config.setSerializerFeatures( // 保留 Map 空的字段 SerializerFeature.WriteMapNullValue, // 將 String 類型的 null 轉(zhuǎn)成"" SerializerFeature.WriteNullStringAsEmpty, // 將 Number 類型的 null 轉(zhuǎn)成 0 SerializerFeature.WriteNullNumberAsZero, // 將 List 類型的 null 轉(zhuǎn)成 [] SerializerFeature.WriteNullListAsEmpty, // 將 Boolean 類型的 null 轉(zhuǎn)成 false SerializerFeature.WriteNullBooleanAsFalse, // 生成的JSON格式化 SerializerFeature.PrettyFormat, // 避免循環(huán)引用 SerializerFeature.DisableCircularReferenceDetect); converter.setFastJsonConfig(config); converter.setDefaultCharset(Charset.forName("UTF-8")); List<MediaType> mediaTypeList = new ArrayList<>(); // 解決中文亂碼問題,相當(dāng)于在 Controller 上的 @RequestMapping 中加了個屬性 produces = "application/json" mediaTypeList.add(MediaType.APPLICATION_JSON); converter.setSupportedMediaTypes(mediaTypeList); converters.add(converter); } }
四、封裝返回的數(shù)據(jù)格式
除了要封裝數(shù)據(jù)之外,我們往往需要在返回的 JSON 中添加一些其他信息,比如返回狀態(tài)碼 Code,返回 Msg 給調(diào)用者,調(diào)用者可以根據(jù) Code 或者 Msg 進行一些邏輯判斷。
統(tǒng)一的 JSON 結(jié)構(gòu)中屬性包括數(shù)據(jù)、狀態(tài)碼、提示信息即可。
public class JsonResult<T> { private T data; private String code; private String msg; /** * 若沒有數(shù)據(jù)返回,默認狀態(tài)碼為 0,提示信息為“操作成功!” */ public JsonResult() { this.code = "0"; this.msg = "操作成功!"; } /** * 若沒有數(shù)據(jù)返回,可以人為指定狀態(tài)碼和提示信息 * @param code * @param msg */ public JsonResult(String code, String msg) { this.code = code; this.msg = msg; } /** * 有數(shù)據(jù)返回時,狀態(tài)碼為 0,默認提示信息為“操作成功!” * @param data */ public JsonResult(T data) { this.data = data; this.code = "0"; this.msg = "操作成功!"; } /** * 有數(shù)據(jù)返回,狀態(tài)碼為 0,人為指定提示信息 * @param data * @param msg */ public JsonResult(T data, String msg) { this.data = data; this.code = "0"; this.msg = msg; } // 省略 get 和 set 方法 }
修改 Controller 中的返回值類型,測試
@RestController @RequestMapping("/jsonresult") public class JsonResultController { @RequestMapping("/user") public JsonResult<User> getUser() { User user = new User(1, "倪升武", "123456"); return new JsonResult<>(user); // {"code":"0","data":{"id":1,"password":"123456","username":"倪升武"},"msg":"操作成功!"} } @RequestMapping("/list") public JsonResult<List> getUserList() { List<User> userList = new ArrayList<>(); User user1 = new User(1, "倪升武", "123456"); User user2 = new User(2, "達人課", "123456"); userList.add(user1); userList.add(user2); return new JsonResult<>(userList, "獲取用戶列表成功"); // {"code":"0","data":[{"id":1,"password":"123456","username":"倪升武"},{"id":2,"password":"123456","username":"達人課"}],"msg":"獲取用戶列表成功"} } @RequestMapping("/map") public JsonResult<Map> getMap() { Map<String, Object> map = new HashMap<>(3); User user = new User(1, "倪升武", null); map.put("作者信息", user); map.put("博客地址", "http://blog.itcodai.com"); map.put("CSDN地址", null); map.put("粉絲數(shù)量", 4153); return new JsonResult<>(map); // {"code":"0","data":{"作者信息":{"id":1,"password":"","username":"倪升武"},"CSDN地址":null,"粉絲數(shù)量":4153,"博客地址":"http://blog.itcodai.com"},"msg":"操作成功!"} } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Spring/SpringBoot?@RequestParam注解無法讀取application/json格式數(shù)據(jù)問題解決
- Springboot?中的?Filter?實現(xiàn)超大響應(yīng)?JSON?數(shù)據(jù)壓縮的方法
- SpringBoot前后端json數(shù)據(jù)交互的全過程記錄
- SpringBoot實現(xiàn)前后端、json數(shù)據(jù)交互以及Controller接收參數(shù)的幾種常用方式
- SpringBoot響應(yīng)Json數(shù)據(jù)亂碼通過配置的解決
- springboot 返回json格式數(shù)據(jù)時間格式配置方式
- 從前端Vue到后端Spring Boot接收JSON數(shù)據(jù)的正確姿勢(常見錯誤及問題)
相關(guān)文章
Java設(shè)計模式之命令模式(Command模式)介紹
這篇文章主要介紹了Java設(shè)計模式之命令模式(Command模式)介紹,本文講解了Command模式的定義、如何使用命令模式等內(nèi)容,需要的朋友可以參考下2015-03-03深入解析Jdk8中Stream流的使用讓你脫離for循環(huán)
這篇文章主要介紹了Jdk8中Stream流的使用,讓你脫離for循環(huán),本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02Springboot實現(xiàn)導(dǎo)入導(dǎo)出Excel的方法
今天帶各位小伙伴學(xué)習(xí)Springboot實現(xiàn)導(dǎo)入導(dǎo)出Excel的方法,文中有非常詳細的介紹,對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05通過Spring Boot + Mybatis + Redis快速搭建現(xiàn)代化Web項目
本篇文章介紹了如何通過Spring Boot、Mybatis以及Redis快速搭建一個現(xiàn)代化的Web項目,并且同時介紹了如何在Spring Boot下優(yōu)雅地書寫單元測試來保證我們的代碼質(zhì)量。具體內(nèi)容詳情大家通過本文學(xué)習(xí)下吧2017-12-12