【问题标题】:Disable redirect to /error for certain urls对某些 url 禁用重定向到 /error
【发布时间】:2017-01-13 20:11:20
【问题描述】:

我创建了一个 springboot 应用程序,该应用程序在 .../api/myEndpoints... 中包含一些 Rest API 端点,以及一些用户可以与之交互的 UI 表单的 thymeleaf 模板。

由于我添加了一个errorController:

@Controller
@RequestMapping("/error")
public class ErrorController {

    @RequestMapping(method = RequestMethod.GET)
    public String index(Model model) {
        return "error";
    }
}

每当在我的 RestControllers 中引发异常时,我都会收到一个包含“错误”一词的空白网站。这可能对 web 前端有意义,但对我的 api 没有意义。对于 API,我希望 spring 输出标准 JSON 结果,例如:

{
    "timestamp": 1473148776095,
    "status": 400,
    "error": "Bad request",
    "exception": "java.lang.IllegalArgumentException",
    "message": "A required parameter is missing (IllegalArgumentException)",
    "path": "/api/greet"
}

当我从 ErrorController 中删除 index 方法时,我总是会收到 JSON 输出。 我的问题是:是否有可能仅针对所有 api url (../api/*) 排除对 /error 的自动重定向?

非常感谢。

【问题讨论】:

    标签: spring spring-boot exception-handling


    【解决方案1】:

    在此之前,可能会有更好的解决方案......以下是实现您所要求的目标的方法:

    (1) 禁用 ErrorMvcAutoConfiguration

    将此添加到您的application.properties

    spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
    

    (2) 定义两个ControllerAdvices

    由于我们禁用了ErrorMvcAutoConfiguration,我们需要自己捕获异常。创建一个通知以捕获特定包的错误,并创建另一个通知以捕获所有其他错误。它们各自重定向到不同的 url。

    //Catch exception for API.
    @ControllerAdvice(basePackageClasses = YourApiController.class)
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public static class ErrorApiAdvice {
        @ExceptionHandler(Throwable.class)
        public String catchApiExceptions(Throwable e) {
            return "/error/api";
        }
    }
    
    //Catch all other exceptions
    @ControllerAdvice
    @Order(Ordered.LOWEST_PRECEDENCE)
    public static class ErrorAdvice {
        @ExceptionHandler(Throwable.class)
        public String catchOtherExceptions() {
            return "/error";
        }
    }
    

    (3)创建一个控制器来处理错误页面

    这是您可以在错误处理中使用不同逻辑的地方:

    @RestController
    public class MyErrorController {
        @RequestMapping("/error/api")
        public String name(Throwable e) {
            return "api error";
        }
    
        @RequestMapping("/error")
        public String error() {
            return "error";
        }
    }
    

    使用 Spring-Boot 1.4.x,您还可以实现 ErrorViewResolver (see this doc):

    @Component
    public class MyErrorViewResolver implements ErrorViewResolver {
        @Override
        public ModelAndView resolveErrorView(HttpServletRequest request,
                HttpStatus status, Map<String, Object> model) {
            if("/one".equals(model.get("path"))){
                return new ModelAndView("/errorpage/api");  
            }else{
                return new ModelAndView("/errorpage");
            }   
        }
    }
    

    【讨论】:

    • 非常感谢您的帮助!!
    • +1 for spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
    猜你喜欢
    • 1970-01-01
    • 2014-11-16
    • 2015-03-02
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2022-01-01
    • 2015-06-12
    相关资源
    最近更新 更多