RestControllerAdvice 不生效问题

m0_46645361 2020-11-12 10:36:30
使用RestControllerAdvice做全局的异常控制,返回我自定义的一个对象。
使用debug模式步进,出现错误的时候能够正常的进入RestControllerAdvice注解下的类中对应ExceptionHandler注解的方法,也返回了我自定义的对象,但是在最后的返回结果中,并不是我自定义的这个对象,而是springmvc的默认错误,Internal Server Error。
上网查了下,得到的答案基本都是错误没正常抛出啊,切面中进行了限制啊什么的,但错误肯定是正常抛出了,要不不会进入到那个异常控制中。切面也只是一些鉴权啊什么的,我也试过把aop都停用掉,但还是不行。
...全文
5348 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
IceMelon123 2022-07-31
  • 打赏
  • 举报
回复

两年多后的今天,我也出现了这个问题。没收到解决方法。 我试着返回一个字符串,发现APIFox是可以接到返回消息的, 然后我就想是不是返回类型不是bean啊? 结果一看,没有加@Data。

weixin_44213243 2021-04-15
  • 打赏
  • 举报
回复
同样的错,解决了麻烦回复一下
weixin_39894437 2021-03-31
  • 打赏
  • 举报
回复
最后解决了吗
luj_1768 2020-12-05
  • 打赏
  • 举报
回复
异常处理与代码运行模式有关,通常调试模式之外的情况可能不支持异常处理机制。如果不是debug模式编译,会自动废弃异常有关代码。而且,advice最多是建议系统使用,非调试模式下通常会被系统忽略。
qkniit 2020-11-13
  • 打赏
  • 举报
回复
接口方法加注解@RequestMapping
liuyh1024 2020-11-13
  • 打赏
  • 举报
回复
我是在Controller返回的也是resul这个类型,如果业务发生异常,在自定义异常捕获里面处理完异常,在由Controller层返给前台。
liuyh1024 2020-11-13
  • 打赏
  • 举报
回复
可以参照一下: https://blog.csdn.net/qq_35098526/article/details/88949425?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
m0_46645361 2020-11-13
  • 打赏
  • 举报
回复
引用 1 楼 liuyh1024 的回复:
/**
 * @author liuyh
 * @Description:
 * @date 2020/9/2214:13
 */
@ControllerAdvice
@Slf4j
public class ExceptionError {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result defaultErrorHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) {
        e.printStackTrace();
        Result resul = new Result();
        if (e instanceof HttpRequestMethodNotSupportedException) {
            resul.setResltInfo(Constant.CODE_TYPE_10, "非法请求", e.getMessage());
        } else if (e instanceof HystrixRuntimeException) {
            resul.setResltInfo(Constant.CODE_TYPE_30, "feign调用系统异常", e.getCause().getMessage());
        } else {
            resul.setResltInfo(Constant.CODE_TYPE_1, "系统异常", e.getMessage());
        }
        return resul;
    }
}
我写的与这个差不多,而且也进到了这个handler里面,运行正常,也正常return了,但是接口返回的东西不是
liuyh1024 2020-11-12
  • 打赏
  • 举报
回复
/**
 * @author liuyh
 * @Description:
 * @date 2020/9/2214:13
 */
@ControllerAdvice
@Slf4j
public class ExceptionError {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result defaultErrorHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) {
        e.printStackTrace();
        Result resul = new Result();
        if (e instanceof HttpRequestMethodNotSupportedException) {
            resul.setResltInfo(Constant.CODE_TYPE_10, "非法请求", e.getMessage());
        } else if (e instanceof HystrixRuntimeException) {
            resul.setResltInfo(Constant.CODE_TYPE_30, "feign调用系统异常", e.getCause().getMessage());
        } else {
            resul.setResltInfo(Constant.CODE_TYPE_1, "系统异常", e.getMessage());
        }
        return resul;
    }
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧