feiniaojin / graceful-response

Spring Boot接口响应处理解决方案,提供统一返回值封装、全局异常处理、自定义异常错误码、参数校验增强、断言增强等功能
https://doc.feiniaojin.com
MIT License
1.12k stars 171 forks source link

通过aop 改变返回值内容,报错类型无法转换 #55

Closed 18894142401 closed 6 months ago

18894142401 commented 6 months ago

项目中有个需要返回加密的内容,通过aop加密后,并改变类型后,报错 2024-05-14 15:09:37.852 ERROR 21704 --- [nio-8089-exec-3] c.f.g.advice.GlobalExceptionAdvice : Graceful Response:GlobalExceptionAdvice捕获到异常,message=[class java.util.HashMap cannot be cast to class com.baomidou.mybatisplus.extension.plugins.pagination.Page (java.util.HashMap is in module java.base of loader 'bootstrap'; com.baomidou.mybatisplus.extension.plugins.pagination.Page is in unnamed module of loader 'app')]

java.lang.ClassCastException: class java.util.HashMap cannot be cast to class com.baomidou.mybatisplus.extension.plugins.pagination.Page (java.util.HashMap is in module java.base of loader 'bootstrap'; com.baomidou.mybatisplus.extension.plugins.pagination.Page is in unnamed module of loader 'app') at top.ko25891wan.springboot2.controller.AdminController$$EnhancerBySpringCGLIB$$1.queryAll() at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) at javax.servlet.http.HttpServlet.service(HttpServlet.java:529) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:623) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:209) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61) at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387) at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at top.ko25891wan.springboot2.common.crypto.filter.HttpServletRequestInputStreamFilter.doFilter(HttpServletRequestInputStreamFilter.java:35) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:168) at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:90) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:41002) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:481) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:390) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:928) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1794) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:834)

18894142401 commented 6 months ago

请问一下,这种需求需要怎么实现?我现在的想法是,以抛出错误的形式,格式化内容,并返回,或者有什么其他的思路?

feiniaojin commented 6 months ago

java.lang.ClassCastException: class java.util.HashMap cannot be cast to class com.baomidou.mybatisplus.extension.plugins.pagination.Page 看您地错误提示是在哪个地方做了强制类型转换,但是转换的过程出错了

18894142401 commented 6 months ago

并不是呢,我展示一下我的代码逻辑 @ApiOperation("查看所有管理者") @GetMapping("checking/admin/queryAll") @EncryptionAnnotation public Page queryAll(@ModelAttribute @Validated PageRequest pageRequest, @ModelAttribute @Validated AdminQueryDto adminQueryDto) { return adminService.queryAll(pageRequest, adminQueryDto); } 下面是加密aop实现 public class EncryptResponseBodyAOP {

@Pointcut("@annotation(top.ko25891wan.springboot2.common.crypto.annotation.EncryptionAnnotation)")
public void point() {
}

@Around("point()")
public Map<?, ?> modifyReturnValue(ProceedingJoinPoint joinPoint) throws Throwable {

    // 执行原方法
    Object result = joinPoint.proceed();
    System.out.println(result);
    Map<?, ?> map = modifyResult(result);
    //        GracefulResponse.wsrapAssert("code", data, () -> Assert.isTrue(id == 1, "id不等于1"));
    System.out.println(map);
    return map;
}

private Map<?, ?> modifyResult(Object originalResult) {
    // 根据需要修改结果

    String jsonPrettyStr = JSONUtil.toJsonPrettyStr(originalResult);
    System.out.println(jsonPrettyStr);
    String s = AESUtil.encryptHex(jsonPrettyStr);
    return MapUtil.of("text", s);
}

}

控制台输出正常 {text=6bfb244de13f9fbdbdde36c2dfa0d67eceeefb73228f6444adbd4b9675bc2833fb3b0b96708bf08c1458cb0fd1d742b1563dd10d87e154da33b50c3d8bcca7f9235823ba9a151d197a7f623acd833746db3ed0ac893fb246858bac1a5dbd5850699cdc120239c9adc29d14cfc34dee69ba910449546e92600dc5d1c2e258d4da6a2291870495f0cea6ba1641b55f80605865d689353b1c9ac8f1c8d76eea00af23b08f3ffbe24599ea5558c836fe9d5753d0dfc738f320efc843ddadaa3ca4c07fa1c4a6478c9c9f7a7c8a01fdcadfb9cd16e3063c239753601ae7ce4f30d59aa0a4ac379bc4cc47a7d9f67d0f6d9de6701b3de4b8f046feb90cf03af52d9551aab33fc9d9f795c0cd0fc44b57c37bb3c4e67385299d1dc4cbd3278a1aad7331919956768e2d144c48fa565e8e4ec443f393075bfc43a8ef44f1308ab963692dfe487ad68fd07d01be1c46ddeac37240a615da04a63a73c1aceae810fe0b097d9cf8e8b34ad9f651ab350e4cd636c444dab4ada8173a67f1111377938fcc4b6d22322af4644682172d28a3c44f4c2d9186e4e15b82b24e4751a814e257612ed72bc802f6e349e0138a310655226b0273fc52edb7547484ffb86ba04a4e85c052622032506c5e3fda9db887fc88cd1cfed65729e1ffe39ba2f8bd6eca1af97b8c79bd1b84c667434343f71dfe3c5dad2a956b53d9993c55ced225949c85efd3cd1a7e716890f37589598312e6577bba076cb5bfb55eeee0111704ae0ddf175c08c5ab5e2cc94c50bb1c62b7016662b50255ca309d838c6b5d954894e5017e57ceddd613fda7e406e81c54f36f9831ccef3a258490d1b4b814355cab4795aa8803c2dcf57e41cdceadd1c3457aadc8a335d3212539d3a124b6041e3956f401115bf33a3f8a804c32653efa48d2164ce26341137b6c6fa977d1462642e3c8dac53e5f52221a6ef82c6adf267f72cd67e11b0c0d54c48c5b65f54e5b830f9506a24d22559fd06487af8fb8e23a858b58f692ff76cb50779c20ec91f3705aa3c3a7d0180e3986ae3c53334fa598a3c7ed9a6daa100f1124b62d6cd8f62b014fd8f615de59f3bb6008e16ce4a0e6fc31f08e38437faa092d176f075800d930c14f7f2863bfa6f9e10d5632ab1d81f773811b39ec55033d9061621e7096ed0ff2e44084a6d9ce71a8b762cb5e40da90da78c2122087bb2ec10c0de04e332192695f2395edaf00d7bd249843884425e4265a8e43d88bd12d33c64c79508ebd8bcc9d615d69c965a41544c5fe126ea1b16e455dea93f618a7a21e2f2212ac075eaf2f8380eab99a73d2a88fe2110123e196cf738b4984c81ec94cfe656182088d1e99a740e0098c8d1b11541c108d418f6124d9afa83158f731ad3a203be4391c590caed9246ccd815b0c0c073718aa6469890a5dcf202ef7df7609d4ff8d4c86adabe1ded82b8d3504ef534b98b62f5f70fb4d98625b9295b41281b468bc1e85f57a1dc1b7764445f21e3c2105fd33e197bc9016e12747cc8c2fccda6787eeacaa7168020f48671a3ac5732d2eaea825410469eb74de00dd1f24c06d9d0d9b7e97609e5c3f4c11a6d472b8b321604739803a38b0dc6215825b208879957087ae3803c3dc3d3631cffa7b4af0fc5da37d8f4f4893d68d523583024692c04c1edf94b193f6662ad209b7fb14bc004f46d631fc1101176e0dfae8a5d3a7d2d35060c4af3133}

18894142401 commented 6 months ago

这个问题可能出在了,返回值是以控制层的返回值为基本类型,处理后续的包装,但是我以aop的形式,转换了类型导致返回错误,请教一下这种情况怎么处理?

feiniaojin commented 5 months ago

你这个问题是,你返回了一个HashMap,但是你把它转成com.baomidou.mybatisplus.extension.plugins.pagination.Page ,就报错了。