博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Springboot使用了ResponseBodyAdvice处理返回值异常?
阅读量:6694 次
发布时间:2019-06-25

本文共 13183 字,大约阅读时间需要 43 分钟。

hot3.png

在使用为了在controller层中只关心我业务数据,而不去关心数据的形式。一般来说,在http接口响应的json数据时,我都回去设置一个响应码code,描述msg,数据data。形式如下:

public class ResponseResult implements Serializable{    private static final long serialVersionUID = 1638693422783183022L;    private String code;    private String msg;    private Object data;    public ResponseResult(CodeConstant codeConstant) {        this.code = codeConstant.getCode();        this.msg = codeConstant.getMsg();    }    public ResponseResult(CodeConstant codeConstant, Object data) {        this(codeConstant);        this.data = data;    }}    

而在controller层中,我只关心我想要返回的数据,也是上面响应实体的部分,我不想在controller中还要处理响应数据包装这种重复无聊的工作。所以controller中我想要的形式是,我只返回我业务需要返回的数据,效果如下:

@RestController@RequestMapping("/auth")@Slf4jpublic class UserController {      @RequestMapping("test1")   public UserEntity test1() {      return new UserEntity();   }      @RequestMapping("test")    public String test2() {      return "12312";   }}

所以为了统一接口响应的报文,我现实了ResponseBodyAdvice接口,通过这个接口的实现类来统一处理报文

public class BaseGlobalResponseBodyAdvice implements ResponseBodyAdvice {   //这个方法表示对于哪些请求要执行beforeBodyWrite,返回true执行,返回false不执行   @Override   public boolean supports(MethodParameter methodParameter, Class aClass) {      return true;   }   //对于返回的对象如果不是最终对象ResponseResult,则选包装一下   @Override   public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType,         Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {      if(!(o instanceof ResponseResult)) {         ResponseResult responseResult = new ResponseResult(CodeConstant.SUCCESS, o);         return responseResult;      }      return o;   }}

然而在测试返回值是String类型的时候,程序抛出一个类转换的异常:

java.lang.ClassCastException: com.jinhualun.moguhou.merchant.console.base.common.ResponseResult cannot be cast to java.lang.String	at org.springframework.http.converter.StringHttpMessageConverter.getContentLength(StringHttpMessageConverter.java:41) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.http.converter.AbstractHttpMessageConverter.addDefaultHeaders(AbstractHttpMessageConverter.java:260) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:205) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:247) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:174) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.16.jar:8.5.16]	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]	at com.jinhualun.moguhou.merchant.console.base.filter.CorsFilter.doFilter(CorsFilter.java:47) [classes/:na]	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.16.jar:8.5.16]	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.16.jar:8.5.16]	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.16.jar:8.5.16]	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

后来经过debug发现这个跟MessageConverter有关系。Springmvc内部定义了九个不同的MessageConverter用来处理不同的返回值。在AbstractMessageConverterMethodProcessor类下面的writeWithMessageConverters方法可以看出来,每个MessageConverer是根据返回类型和媒体类型来选择处理的MessageConverter的,下面是代码片段:

if (selectedMediaType != null) {   selectedMediaType = selectedMediaType.removeQualityValue();   for (HttpMessageConverter
messageConverter : this.messageConverters) { if (messageConverter instanceof GenericHttpMessageConverter) { if (((GenericHttpMessageConverter) messageConverter).canWrite( declaredType, valueType, selectedMediaType)) { outputValue = (T) getAdvice().beforeBodyWrite(outputValue, returnType, selectedMediaType, (Class
>) messageConverter.getClass(), inputMessage, outputMessage); if (outputValue != null) { addContentDispositionHeader(inputMessage, outputMessage); ((GenericHttpMessageConverter) messageConverter).write( outputValue, declaredType, selectedMediaType, outputMessage); if (logger.isDebugEnabled()) { logger.debug("Written [" + outputValue + "] as \"" + selectedMediaType + "\" using [" + messageConverter + "]"); } } return; } } else if (messageConverter.canWrite(valueType, selectedMediaType)) { outputValue = (T) getAdvice().beforeBodyWrite(outputValue, returnType, selectedMediaType, (Class
>) messageConverter.getClass(), inputMessage, outputMessage); if (outputValue != null) { addContentDispositionHeader(inputMessage, outputMessage); ((HttpMessageConverter) messageConverter).write(outputValue, selectedMediaType, outputMessage); if (logger.isDebugEnabled()) { logger.debug("Written [" + outputValue + "] as \"" + selectedMediaType + "\" using [" + messageConverter + "]"); } } return; } }}

所以导致这个问题的原因就是,controller层中返回的类型是String,但是在ResponseBodyAdvice实现类中,我们把响应的类型修改成了ResponseResult。这就导致了,上面的这段代码在选择处理MessageConverter的时候,依旧根据之前的String类型选择对应String类型的StringMessageConverter。而在StringMessageConverter类型,他只接受String类型的返回类型,我们在ResponseBodyAdvice中将返回值从String类型改成ResponseResult类型之后,调用StringMessageConverter方法发生类型强转。ReponseResult无法转换成String,发生类型转换异常。

 

所以解决该异常最好的方式就是重写StringMessageConverter方法,让他可以解决ResonseResult类型的转化。后来我没有用这种方式,我觉得这样相对比较麻烦,所以换了一种思路,在ResponseBodyAdvice中做了针对String类型返回值的修改:

public class BaseGlobalResponseBodyAdvice implements ResponseBodyAdvice {   //这个方法表示对于哪些请求要执行beforeBodyWrite,返回true执行,返回false不执行   @Override   public boolean supports(MethodParameter methodParameter, Class aClass) {      return true;   }   //对于返回的对象如果不是最终对象ResponseResult,则选包装一下   @Override   public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType,         Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {      if(!(o instanceof ResponseResult)) {         ResponseResult responseResult = new ResponseResult(CodeConstant.SUCCESS, o);         //因为handler处理类的返回类型是String,为了保证一致性,这里需要将ResponseResult转回去         if(o instanceof String) {            return JSON.toJSONString(responseResult);         }         return responseResult;      }      return o;   }}

最终解决了这个问题。

 

 

 

 

 

 

转载于:https://my.oschina.net/u/1757225/blog/1543715

你可能感兴趣的文章
设计模式--FACADE
查看>>
wsdl
查看>>
手机LED---点阵字体实现
查看>>
新项目要考虑的问题
查看>>
URL中“#” “?” &“”号的作用
查看>>
Linux: 字体安装
查看>>
ImportError: No module named MySQLdb
查看>>
MySQL主从复制与都系分离
查看>>
远程连接MySQL, 10038问题
查看>>
ACPI电源管理中的S0 S1 S2 S3 S4 S5
查看>>
Eclipse 全屏插件及颜色主题插件
查看>>
动态数据类型转换
查看>>
wordpress修改域名后的一些设置:
查看>>
VS2012 GetTickCount64
查看>>
jquery常用
查看>>
在 CentOS 7 中安装 Nextcloud
查看>>
iOS 发送邮件 ios7
查看>>
JavaMailSenderImpl
查看>>
【Android】EditText的特殊属性介绍
查看>>
go处理json格式文件
查看>>