本文小編為大家詳細(xì)介紹“spring boot全局統(tǒng)一返回RESTful風(fēng)格數(shù)據(jù)、統(tǒng)一異常處理的方法”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“spring boot全局統(tǒng)一返回RESTful風(fēng)格數(shù)據(jù)、統(tǒng)一異常處理的方法”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
成都創(chuàng)新互聯(lián)是由多位在大型網(wǎng)絡(luò)公司、廣告設(shè)計(jì)公司的優(yōu)秀設(shè)計(jì)人員和策劃人員組成的一個(gè)具有豐富經(jīng)驗(yàn)的團(tuán)隊(duì),其中包括網(wǎng)站策劃、網(wǎng)頁(yè)美工、網(wǎng)站程序員、網(wǎng)頁(yè)設(shè)計(jì)師、平面廣告設(shè)計(jì)師、網(wǎng)絡(luò)營(yíng)銷人員及形象策劃。承接:成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站改版、網(wǎng)頁(yè)設(shè)計(jì)制作、網(wǎng)站建設(shè)與維護(hù)、網(wǎng)絡(luò)推廣、數(shù)據(jù)庫(kù)開發(fā),以高性價(jià)比制作企業(yè)網(wǎng)站、行業(yè)門戶平臺(tái)等全方位的服務(wù)。
全局統(tǒng)一返回RESTful風(fēng)格數(shù)據(jù),主要是實(shí)現(xiàn)ResponseBodyAdvice接口的方法,對(duì)返回值在輸出之前進(jìn)行修改。
使用注解@RestControllerAdvice攔截異常并統(tǒng)一處理。
開發(fā)環(huán)境:
IntelliJ IDEA 2019.2.2
jdk1.8
Spring Boot 2.2.2
1、創(chuàng)建一個(gè)SpringBoot項(xiàng)目,pom.xml引用的依賴包如下
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency>
2、定義一個(gè)返回類
package com.example.response.entity; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import java.io.Serializable; @Data @NoArgsConstructor @ToString public class ResponseData<T> implements Serializable { /** * 狀態(tài)碼:0-成功,1-失敗 * */ private int code; /** * 錯(cuò)誤消息,如果成功可為空或SUCCESS * */ private String msg; /** * 返回結(jié)果數(shù)據(jù) * */ private T data; public static ResponseData success() { return success(null); } public static ResponseData success(Object data) { ResponseData result = new ResponseData(); result.setCode(0); result.setMsg("SUCCESS"); result.setData(data); return result; } public static ResponseData fail(String msg) { return fail(msg,null); } public static ResponseData fail(String msg, Object data) { ResponseData result = new ResponseData(); result.setCode(1); result.setMsg(msg); result.setData(data); return result; } }
3、統(tǒng)一攔截接口返回?cái)?shù)據(jù)
新建一個(gè)類GlobalResponseHandler,用注解@RestControllerAdvice,并且實(shí)現(xiàn)ResponseBodyAdvice接口的方法,其中方法supports可以判斷哪些需要攔截,方法beforeBodyWrite可以對(duì)返回值在輸出之前進(jìn)行修改,從而實(shí)現(xiàn)返回統(tǒng)一的接口數(shù)據(jù)。
package com.example.response.config; import com.alibaba.fastjson.JSON; import com.example.response.entity.ResponseData; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; /** * 實(shí)現(xiàn)ResponseBodyAdvice接口,可以對(duì)返回值在輸出之前進(jìn)行修改 */ @RestControllerAdvice public class GlobalResponseHandler implements ResponseBodyAdvice<Object> { //判斷支持的類型 @Override public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) { // 檢查注解是否存在,存在則忽略攔截 if (methodParameter.getDeclaringClass().isAnnotationPresent(IgnorReponseAdvice.class)) { return false; } if (methodParameter.getMethod().isAnnotationPresent(IgnorReponseAdvice.class)) { return false; } return true; } @Override public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { // 判斷為null構(gòu)建ResponseData對(duì)象進(jìn)行返回 if (o == null) { return ResponseData.success(); } // 判斷是ResponseData子類或其本身就返回Object o本身,因?yàn)橛锌赡苁墙涌诜祷貢r(shí)創(chuàng)建了ResponseData,這里避免再次封裝 if (o instanceof ResponseData) { return (ResponseData<Object>) o; } // String特殊處理,否則會(huì)拋異常 if (o instanceof String) { return JSON.toJSON(ResponseData.success(o)).toString(); } return ResponseData.success(o); } }
新建自定義注解IgnorReponseAdvice
package com.example.response.config; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface IgnorReponseAdvice { }
4、統(tǒng)一異常處理
package com.example.response.exception; import com.example.response.entity.ResponseData; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseData exceptionHandler(Exception e) { e.printStackTrace(); return ResponseData.fail("服務(wù)器異常:" + e.getMessage()); } }
5、新建一個(gè)測(cè)試用的實(shí)體類
package com.example.response.entity; import lombok.Data; @Data public class User { private Long userId; private String userName; public User(Long userId, String userName){ this.userId = userId; this.userName = userName; } }
6、新建一個(gè)測(cè)試用的控制器類
package com.example.response.controller; import com.example.response.config.IgnorReponseAdvice; import com.example.response.entity.ResponseData; import com.example.response.entity.User; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; @RestController public class DemoController { @GetMapping("user") public User user() { User u = new User(100L, "u1"); return u; } @GetMapping("userList") public List<User> userList(){ List<User> list = new ArrayList<User>(); list.add(new User(100L, "u1")); list.add(new User(200L, "u2")); return list; } @GetMapping("test1") public String test1(){ return "test1"; } @GetMapping("test2") public ResponseData test2(){ return ResponseData.success("test2"); } @IgnorReponseAdvice @GetMapping("test3") public String test3() { return "test3"; } @GetMapping("test4") public String test4() { Integer x = 1 / 0; return x.toString(); } @GetMapping("test5") public String test5() throws Exception { throw new Exception("自定義異常信息"); } }
7、用Postman測(cè)試
(1)請(qǐng)求http://localhost:8080/user,返回
{ "code": 0, "msg": "SUCCESS", "data": { "userId": 100, "userName": "u1" } }
(2)請(qǐng)求http://localhost:8080/userList,返回
{ "code": 0, "msg": "SUCCESS", "data": [ { "userId": 100, "userName": "u1" }, { "userId": 200, "userName": "u2" } ] }
(3)請(qǐng)求http://localhost:8080/tes1,返回
{"msg":"SUCCESS","code":0,"data":"test1"}
(4)請(qǐng)求http://localhost:8080/test2,返回
{ "code": 0, "msg": "SUCCESS", "data": "test2" }
(5)請(qǐng)求http://localhost:8080/test3,返回
test3
(6)請(qǐng)求http://localhost:8080/test4,返回
{ "code": 1, "msg": "服務(wù)器異常:/ by zero", "data": null }
(7)請(qǐng)求http://localhost:8080/test5,返回
{ "code": 1, "msg": "服務(wù)器異常:自定義異常信息", "data": null }
讀到這里,這篇“spring boot全局統(tǒng)一返回RESTful風(fēng)格數(shù)據(jù)、統(tǒng)一異常處理的方法”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:springboot全局統(tǒng)一返回RESTful風(fēng)格數(shù)據(jù)、統(tǒng)一異常處理的方法
當(dāng)前路徑:http://www.rwnh.cn/article22/pgchjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)、App開發(fā)、云服務(wù)器、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)