0

我有这样的异常处理程序:

@ControllerAdvice
public classMyExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(MyRuntimeException.class)
    public String handleMyRuntimeException(MyRuntimeExceptionexception ex){
        LOGGER.info(ex.getMessage());
        return "redirect:/www.google.com";
    }
}

我执行 http 请求,我看到 Controller 处理了我的请求,然后MyRuntimeException正在抛出并且handleMyRuntimeException正在调用方法。但在邮递员中,我看到服务器返回 401 http 状态,而我在响应标头中看不到 www.google.com。

我错了什么?

4

2 回答 2

1

首先,Postman 默认会自动跟随重定向。您在 Postman 中得到的是已经重定向到的响应/www.google.com。转到设置将其关闭:

在此处输入图像描述

redirect:/www.google.com是不同redirect://www.google.com 。假设您的服务器是127.0.0.1:8080

  • redirect:/www.google.com --> 重定向到 http://127.0.0.1:8080/www.google.com
  • redirect://www.google.com--> 重定向到http://www.google.com

所以你实际上重定向回你的服务器,你收到的 401 错误可能是由于你的服务器的访问控制。

于 2019-01-29T10:14:34.283 回答
0

就我而言,它工作正常,请检查:

@ControllerAdvice
public class ErrorHandler {

    @ExceptionHandler(CustomRuntimeException.class)
    @ResponseStatus(value=HttpStatus.OK)
    public ModelAndView handleCustomRuntimeException(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        ModelAndView mav = new ModelAndView("error");
        mav.addObject("error", "500");
        return mav;
        //return new ModelAndView("redirect:https://www.google.com");
    }
}
于 2019-01-29T11:15:42.383 回答