ARTS打卡计划第一周-Tips-ControllerAdvice的使用
通常在开发具体项目过程中我们可能会面临如下问题:
- 统一所有的json返回结果
- 统一处理所有controller中的异常,并且给不同异常不同的返回状态值
- 统一对返回的接口做数据校验或者加密,防止篡改
在spring中的处理方式是使用@RestControllerAdvice注解。下面是一个例子,可以将所有的controller中的返回结果,包装成一个CommonResponse。
@RestControllerAdvice
public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> { @Override
@SuppressWarnings("all")
public boolean supports(MethodParameter methodParameter,
Class<? extends HttpMessageConverter<?>> aClass) { if (methodParameter.getDeclaringClass().isAnnotationPresent(
IgnoreResponseAdvice.class
)) {
return false;
} if (methodParameter.getMethod().isAnnotationPresent(
IgnoreResponseAdvice.class
)) {
return false;
} return true;
} @Nullable
@Override
@SuppressWarnings("all")
public Object beforeBodyWrite(@Nullable Object o,
MethodParameter methodParameter,
MediaType mediaType,
Class<? extends HttpMessageConverter<?>> aClass,
ServerHttpRequest serverHttpRequest,
ServerHttpResponse serverHttpResponse) { CommonResponse<Object> response = new CommonResponse<>(0, "");
if (null == o) {
return response;
} else if (o instanceof CommonResponse) {
response = (CommonResponse<Object>) o;
} else {
response.setData(o);
} return response;
}
}
上述代码中定义了一个注解IgnoreResponseAdvice,如果controller的类或者方法有这个注解就不做处理。下面这个例子展现的是如何在controller抛出异常的时候,自动包装成为commonRespons。
@RestControllerAdvice
public class GlobalExceptionAdvice { @ExceptionHandler(value = ParamException.class)
public CommonResponse<String> handlerParamException(HttpServletRequest req,
ParamException ex) {
CommonResponse<String> response = new CommonResponse<>(400,
"param error");
response.setData(ex.getMessage());
return response;
}
@ExceptionHandler(value = BusinessException.class)
public CommonResponse<String> handlerBusinessException(HttpServletRequest req,
BusinessException ex) {
CommonResponse<String> response = new CommonResponse<>(500,
"business error");
response.setData(ex.getMessage());
return response;
} @ExceptionHandler(value = SystemException.class)
public CommonResponse<String> handlerSystemException(HttpServletRequest req,
SystemException ex) {
CommonResponse<String> response = new CommonResponse<>(700,
"system error");
response.setData(ex.getMessage());
return response;
}
}
对比下面的controller能更清楚的明白如何使用。
@RestController
public class IndexController { @RequestMapping("/")
String home() {
return "Hello World!";
}
@IgnoreResponseAdvice
@RequestMapping("/hi")
String hi() {
return "Hello World!";
} @RequestMapping("/param")
String param() throws Exception {
throw new ParamException("参数错误");
} @RequestMapping("/business")
String business() throws Exception {
throw new BusinessException("业务错误");
}
@RequestMapping("/system")
String system() throws Exception {
throw new SystemException("系统错误");
}
}
详细的代码见 https://gitee.com/dongqihust/arst/tree/master/custom-response
特别的,这个文章提供了几种其他的解决方案:https://www.baeldung.com/exception-handling-for-rest-with-spring
ARTS打卡计划第一周-Tips-ControllerAdvice的使用的更多相关文章
- ARTS打卡计划第一周
Algorithms: https://leetcode-cn.com/problems/two-sum/ Review: https://www.infoq.cn/article/EafgGJEtq ...
- ARTS打卡计划第一周-Share-系统字典模块的设计
在软件开发的过程,经常有一些类型的字段信息:性别.学历.职级.车辆类别.公司类型.结算类型等.这些字段有2个特征:1是字段可选的类型是有限,2是字段可能会变化,我们把这种字段描述为字段字段. 本篇文 ...
- ARTS打卡计划第一周-Review
本周分享的文章来自于medium的 Testing Best Practices for Java + Spring Apps 这个文章主要讲的是java测试的一些最佳实践 1.避免函数返回void, ...
- ARTS打卡计划第一周-Algorithm
7. Reverse Integer import math class Solution: def reverse(self, x: int) -> int: ret = 0 if x > ...
- ARTS打卡计划第二周
Algorithms: https://leetcode-cn.com/problems/3sum/ 算法是先排序,然后按照两个数和两边逼中,考虑去重. Review: https://www.inf ...
- ARTS打卡计划第二周-Review
本周review的文章是:https://medium.com/@hakibenita/optimizing-django-admin-paginator-53c4eb6bfca3 改篇文章的题目是: ...
- ARTS打卡计划第九周
Algorithms: https://leetcode-cn.com/problems/merge-two-sorted-lists/submissions/ 合并两个链表 Review: “Pu ...
- ARTS打卡计划第二周-Share-使用java注解对方法计时
现在有这样一种常见,系统中有一个接口,该接口执行的方法忽快忽慢,因此你需要去统计改方法的执行时间.刚开始你的代码可能如下: long start = System.currentTimeMillis( ...
- ARTS打卡计划第二周-Tips-mysql-binlog-connector-java的使用
最近发现一个挺不错的框架mysql-binlog-connector-java,可以实时监控binlog的变化. 首先检查mysql的binlog是否开启,在开启的情况下: 引入依赖 <depe ...
随机推荐
- django 的后台管理
class bcb(models.Model): name = models.CharField(max_length=64,verbose_name='班次名称') verbose_name = ' ...
- 正则求解@" (?<=^\[length=)(\d+)(?=\])"
举个例子 [length=1548]这个正则 就是匹配 length的值了(1548)(?<=exp)匹配之后的(?=exp)匹配表达式之前的^是边界,在行首例如 aa[length=1548] ...
- python爬虫学习笔记(一)——环境配置(windows系统)
在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库) [推荐地址:清华镜像] https://mirrors ...
- nodejs静态web服务
项目准备 Web 服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,可以向浏览器等 Web 客户端提供文档,也可以放置网站文件,让全世界浏览:可以放置数据文件,让全世界下载.目前最主流的 ...
- dubbo 在不同协议下携带上下文区别
如果走原生的dubbo协议,RpcContext.getContext()里的attarchments和values 是能够在节点间传递的 但如果hessian协议,attarchments和valu ...
- visual studio 2015引入开源控件DockPanel(最简单的方法)
一.DockPanel简介 DockPanel是一个开源控件,能够实现子窗口的浮动,在官方给的demo有演示,在vs2017微软已经集成进入常用控件中.我主要使用的是多窗口浮动,和tabControl ...
- 03-自己封装DateUtil工具类
package com.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.u ...
- 刘志梅201771010115.《面向对象程序设计(java)》第十四周学习总结
实验十四 Swing图形界面组件 实验时间 20178-11-29 1.实验目的与要求 (1)设计模式:反复使用.经过分类编目的.代码设计经验的总结. 在Alexander的模式分类和软件模式的分类 ...
- 记一次bond引起的网络故障
本案中3个关键服务器 物理服务器:192.168.6.63,简称P,(Physical server) KVM-VM:192.168.6.150,是物理服务器P上的一个KVM虚机,简称VM NAS:外 ...
- redis-cli显示中文
在启动redis-cli时在其后面加上--raw参数即可启动后 再显示就正常了