spring boot 2 全局统一返回RESTful风格数据、统一异常处理
全局统一返回restful风格数据,主要是实现responsebodyadvice接口的方法,对返回值在输出之前进行修改。
使用注解@restcontrolleradvice拦截异常并统一处理。
开发环境:
intellij idea 2019.2.2
jdk1.8
spring boot 2.2.2
1、创建一个springboot项目,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、定义一个返回类
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 { /** * 状态码:0-成功,1-失败 * */ private int code; /** * 错误消息,如果成功可为空或success * */ private string msg; /** * 返回结果数据 * */ 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、统一拦截接口返回数据
新建一个类globalresponsehandler,用注解@restcontrolleradvice,并且实现responsebodyadvice接口的方法,其中方法supports可以判断哪些需要拦截,方法beforebodywrite可以对返回值在输出之前进行修改,从而实现返回统一的接口数据。
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; /** * 实现responsebodyadvice接口,可以对返回值在输出之前进行修改 */ @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构建responsedata对象进行返回 if (o == null) { return responsedata.success(); } // 判断是responsedata子类或其本身就返回object o本身,因为有可能是接口返回时创建了responsedata,这里避免再次封装 if (o instanceof responsedata) { return (responsedata<object>) o; } // string特殊处理,否则会抛异常 if (o instanceof string) { return json.tojson(responsedata.success(o)).tostring(); } return responsedata.success(o); } }
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、统一异常处理
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("服务器异常:" + e.getmessage()); } }
5、新建一个测试用的实体类
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、新建一个测试用的控制器类
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测试
(1)请求http://localhost:8080/user,返回
{ "code": 0, "msg": "success", "data": { "userid": 100, "username": "u1" } }
(2)请求http://localhost:8080/userlist,返回
{ "code": 0, "msg": "success", "data": [ { "userid": 100, "username": "u1" }, { "userid": 200, "username": "u2" } ] }
(3)请求http://localhost:8080/tes1,返回
{"msg":"success","code":0,"data":"test1"}
(4)请求http://localhost:8080/test2,返回
{ "code": 0, "msg": "success", "data": "test2" }
(5)请求http://localhost:8080/test3,返回
test3
(6)请求http://localhost:8080/test4,返回
{ "code": 1, "msg": "服务器异常:/ by zero", "data": null }
(7)请求http://localhost:8080/test5,返回
{ "code": 1, "msg": "服务器异常:自定义异常信息", "data": null }
上一篇: 豆浆机营养食谱有哪些
下一篇: 如何通过游戏内的嘟嘟语音进入指定的频道
推荐阅读
-
spring boot 2 全局统一返回RESTful风格数据、统一异常处理
-
RestFul API 统一格式返回 + 全局异常处理
-
只需一步,在Spring Boot中统一Restful API返回值格式与统一处理异常
-
Spring Boot全局统一异常处理器
-
spring boot2.x 后端参数校验+统一异常处理+后端自定义全局统一接口返回响应数据格式
-
spring boot 2 全局统一返回RESTful风格数据、统一异常处理
-
Spring Boot处理全局统一异常的两种方法与区别
-
8 Spring Boot返回数据及异常统一封装
-
RestFul API 统一格式返回 + 全局异常处理
-
Spring Boot 2.X REST 风格全局异常处理