SpringController返回值和异常自动包装

今天遇到一个需求,在不改动原系统代码的情况下。将Controller的返回值和异常包装到一个统一的返回对象中去。

例如原系统的接口

public String myIp(@ApiIgnore HttpServletRequest request);

返回的只是一个IP字符串"0:0:0:0:0:0:0:1",目前接口需要包装为:

{"code":200,"message":"","result":"0:0:0:0:0:0:0:1","success":true}

而原异常跳转到error页面,需要调整为

{
  "success": false,
  "message": "For input string: \"fdsafddfs\"",
  "code": 500,
  "result": "message"
}

因此就有了2个工作子项需要完成:

1)Exception的处理

2)controller return值的处理

Exception的自动包装

返回的exception处理可以采用@RestControllerAdvice来处理。

建立自己的Advice类,注入国际化资源(异常需要支持多语言)
 

package org.ccframe.commons.mvc;import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.ccframe.commons.filter.CcRequestLoggingFilter;
import org.ccframe.commons.util.BusinessException;
import org.ccframe.config.GlobalEx;
import org.ccframe.subsys.core.dto.Result;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.core.MethodParameter;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.NoHandlerFoundException;import javax.servlet.http.HttpServletRequest;
import java.util.Locale;@RestControllerAdvice
@Log4j2
public class GlobalRestControllerAdvice{private MessageSource messageSource; //国际化资源private LocaleResolver localeResolver;private Object[] EMPTY_ARGS = new Object[0];public GlobalRestControllerAdvice(MessageSource messageSource, LocaleResolver localeResolver){this.messageSource = messageSource;this.localeResolver = localeResolver;}private Result<String> createError(HttpServletRequest request, Exception e,int code, String msgKey, Object[] args){Locale currentLocale = localeResolver.resolveLocale(request);String message = "";try {message = messageSource.getMessage(msgKey, args, currentLocale);}catch (NoSuchMessageException ex){message = e.getMessage();}finally {log.error(message);CcRequestLoggingFilter.pendingLog(); //服务器可以记录出错时的请求啦😂}return Result.error(code, message, msgKey);}@ExceptionHandler(NoHandlerFoundException.class)public Result<?> handlerNoFoundException(HttpServletRequest request, Exception e) {return createError(request, e, HttpStatus.SC_NOT_FOUND, "error.mvc.uriNotFound", EMPTY_ARGS);}@ExceptionHandler(HttpRequestMethodNotSupportedException.class)public Result<?> httpRequestMethodNotSupportedException(HttpServletRequest request, HttpRequestMethodNotSupportedException e){return createError(request,e, HttpStatus.SC_NOT_FOUND,"error.mvc.methodNotSupported",new Object[]{e.getMethod(), StringUtils.join(e.getSupportedMethods(), GlobalEx.DEFAULT_TEXT_SPLIT_CHAR)});}@ExceptionHandler(BusinessException.class)public Result<?> businessException(HttpServletRequest request, BusinessException e){return createError(request,e, HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMsgKey(), e.getArgs());}@ExceptionHandler(ObjectOptimisticLockingFailureException.class) //乐观锁异常public Result<?> objectOptimisticLockingFailureException(HttpServletRequest request, ObjectOptimisticLockingFailureException e){return createError(request,e, HttpStatus.SC_INTERNAL_SERVER_ERROR, "errors.db.optimisticLock", EMPTY_ARGS);}@ExceptionHandler(MaxUploadSizeExceededException.class) // 文件上传超限,nginx请设置为10Mpublic Result<?> handleMaxUploadSizeExceededException(HttpServletRequest request, MaxUploadSizeExceededException e) {return createError(request, e, HttpStatus.SC_INTERNAL_SERVER_ERROR, "error.mvc.fileTooLarge", EMPTY_ARGS);}@ExceptionHandler(Exception.class)@ResponseBodypublic Result<?> handleException(HttpServletRequest request, Exception e) {log.error(e);return createError(request,e, HttpStatus.SC_INTERNAL_SERVER_ERROR, "message", new Object[]{e.getMessage()});}
}

在Config类初始化该Bean(当然也可以使用@Component支持扫描,随你喜欢)
 

	@Beanpublic GlobalRestControllerAdvice globalRestControllerAdvice(MessageSource messageSource, LocaleResolver localeResolver){return new GlobalRestControllerAdvice(messageSource, localeResolver);}

controller return值的自动包装

网上的例子有很多坑,包括使用HandlerMethodReturnValueHandler,看了源码才发现。还是ResponseBodyAdvice好使。

建立自己的处理Bean

package org.ccframe.commons.mvc;import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import org.apache.http.HttpStatus;
import org.ccframe.commons.util.JsonUtil;
import org.ccframe.subsys.core.dto.Result;
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.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import springfox.documentation.swagger.web.ApiResourceController;import java.util.regex.Pattern;@ControllerAdvice
public class CcResponseBodyAdvice implements ResponseBodyAdvice<Object> {private static final Pattern CONTROLLER_PATTERN = Pattern.compile("^org\\.ccframe\\.(subsys|sdk)\\.[a-z0-9]+\\.controller\\.");@Overridepublic boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {return // 只有自己的cotroller类才需要进入,否则swagger都会挂了
CONTROLLER_PATTERN.matcher(returnType.getContainingClass().getName()).find();}@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {System.out.println(returnType.getContainingClass());Result<Object> result = new Result<>();result.setResult(body);result.setCode(HttpStatus.SC_OK);if(body instanceof String){ //String返回要特殊处理return JSON.toJSONString(result);}else {return result;}}
}

如果你不需要根据正则来指定包,可以直接用RestControllerAdvice的basePackages属性来过滤

注意这里有2个坑

1)String类型的返回被其它的转换接口StringHttpMessageConverter处理,因此返回要进行JSON编码而不能返回其他类型,否则会报cast类型错,因此就有了String部分的特殊处理方法。

2)controller方法签名返回是void时,不会被处理。为什么,有什么办法?得看spring这段源码:

当returnValue==null时,设置为RequestHandled,也就是提前结束了。后面任何返回的处理都不再进行。所以,如果一定要返回null值的话,可以在controller里返回一个
return new ResponseEntity<Void>(HttpStatus.OK);
这样在返回的值里面就有详细的结构了。

最后要生效的话,在Config类初始它:
 

	@Beanpublic CcResponseBodyAdvice ccResponseBodyAdvice() {return new CcResponseBodyAdvice();}

最后。上面两个Bean也可以写在一个,有兴趣的自己尝试。

---------------

null无法被BodyAdvice处理的问题。随着源码跟踪,慢慢知道怎么回事了,我们尝试根本来解决这个问题。从这个图开始:


当返回为null时,mavContainer.isRequestHandled()为true导致了后面的没有处理。

那么想当然的,mavContainer.isRequestHandled()为flase不久解决了吗,向前跟踪,基本到MVC invoke的核心代码里了,发现在invoke前,mavContainer.isRequestHandled()变成了true,再继续跟踪,找到这个方法:

在HandlerMethodArgumentResolverComposite的argumentResolvers看到了上面这个。进行了setRequestHandled。HandlerMethodArgumentResolver是spring controller的参数自动注入机制。看了下源码也没有太多的扩展点,于是只能换个思路。由于是执行方法时是反射Invoke处理的:
return getBridgedMethod().invoke(getBean(), args);
因此void类型也无法接收任何的参数,AOP也不可行。因此除了覆盖改源码没有更好的扩展方法。

最后想了下,还是放弃修改。

如果controller确实要返回void,可以使用

return new ResponseEntity<Void>(HttpStatus.OK);

来替代


 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/535066.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

t_t 你们到底是从哪儿找的那种拿我当制杖一样教的简单的纯HTML模板的呀?

在这个万物vue的年代&#xff0c;网页设计越来越框架化。 上网搜个资料学习学习吧&#xff0c;咵咵咵&#xff0c;“游泳健身&#xff0c;vue了解一下” 我只是想简单地学个html&#xff0c;js啊&#xff01;怎么就这么复杂&#xff01; 曾几何时&#xff0c;在网上找个网页…

I2C学习总结

i2c概述 I2C&#xff08;Inter-Intergreted Circuit&#xff09; 是一种串行通信协议&#xff0c;用于集成电路之间完成数据传输&#xff0c;i2c用广泛用以各种领域&#xff0c;包括电子设备、嵌入式系统、工业自动化等&#xff1b; i2c仅仅只是一个数据传输的协议&#xff0c…

Splitpanes拆分窗格插件使用

目录 基本用法 纵向排列 遍历渲染 动态拆分宽度 项目开发中用到了拆分窗格(就是下面的效果&#xff0c;可以拆分网页&#xff0c;我们项目通常都是用左右两块拆分&#xff0c;可以通过拖动图标进行左右拖动)&#xff0c;于是就发现了一个很好用的插件&#xff1a;Splitpane…

2024年AI辅助研发:科技创新的引擎

CSND - 个人主页&#xff1a;17_Kevin-CSDN博客 收录专栏&#xff1a;《人工智能》 技术进展 进入2024年&#xff0c;人工智能&#xff08;AI&#xff09;在科技界和工业界的焦点地位更加巩固&#xff0c;其在辅助研发领域的技术进步尤为显著。深度学习技术的突飞猛进使得数据分…

1.Python是什么?——《跟老吕学Python编程》

1.Python是什么&#xff1f;——《跟老吕学Python编程》 Python是一种什么样的语言&#xff1f;Python的优点Python的缺点 Python发展历史Python的起源Python版本发展史 Python的价值学Python可以做什么职业&#xff1f;Python可以做什么应用&#xff1f; Python是一种什么样的…

图片格式转换怎么操作?这一个方法快快收藏

图片格式转换能够改变图片的质量、大小兼容性。不同的图片格式用途也不同&#xff0c;当我们需要转换图片格式的时候要怎么操作呢&#xff1f;下面&#xff0c;小编给大家分享一款操作简单&#xff0c;小白也能轻松上手的图片转换器&#xff08;https://www.yasuotu.com/geshi&…

“光谱视界革新:ChatGPT在成像光谱遥感中的智能革命“

遥感技术主要通过卫星和飞机从远处观察和测量我们的环境&#xff0c;是理解和监测地球物理、化学和生物系统的基石。ChatGPT是由OpenAI开发的最先进的语言模型&#xff0c;在理解和生成人类语言方面表现出了非凡的能力。本文重点介绍ChatGPT在遥感中的应用&#xff0c;人工智能…

【SpringBoot】整合Druid数据源和Mybatis 项目打包和运行

文章目录 一、整合Druid数据源二、整合Mybatis2.1 MyBatis整合步骤2.1 Mybatis整合实践2.1 声明式事务整合配置2.1 AOP整合配置 三、项目打包和运行命令启动和参数说明 总结web 与 springboot 打包区别JDK8的编译环境 执行17高版本jar 一、整合Druid数据源 创建模块 &#xff1…

BEVFormer代码运行笔记

1. 代码下载 git clone https://github.com/fundamentalvision/BEVFormer.git 2. 环境配置 使用conda创建环境 conda create -n open-mmlab python3.8 -y 进入环境 conda activate open-mmlab 允许使用pip安装依赖库 export PIP_REQUIRE_VIRTUALENVfalse 安装pytorch和…

FPGA - 单总线协议(one-wire)

1&#xff0c;简介 单总线&#xff08;one-wire&#xff09;是美国 DALLAS 公司推出的外围串行扩展总线技术&#xff0c;与 SPI、I2C 等串行数据通信方式不同&#xff0c;它采用单根信号线&#xff0c;既传输时钟又传输数据&#xff0c;而且数据传输是双向的。它具有节省 I/O口…

如何使用ChatGPT辅助写论文、数据分析、AI绘图?【附学习资料】

原文链接&#xff1a;如何使用ChatGPT辅助写论文、数据分析、AI绘图&#xff1f;【附学习资料】https://mp.weixin.qq.com/s?__bizMzUzNTczMDMxMg&mid2247597452&idx1&sn8aa991235ffee89fc76590a90b9005d7&chksmfa823c6bcdf5b57df0cb02ecd1821921f38ea6de34c7…

案例--某站视频爬取

众所周知&#xff0c;某站的视频是&#xff1a; 由视频和音频分开的。 所以我们进行获取&#xff0c;需要分别获得它的音频和视频数据&#xff0c;然后进行音视频合并。 这么多年了&#xff0c;某站还是老样子&#xff0c;只要加个防盗链就能绕过。&#xff08;防止403&#xf…