Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot@ControllerAdvice异常处理程序未启动_Java_Spring_Spring Mvc_Spring Boot_Kotlin - Fatal编程技术网

Java Spring Boot@ControllerAdvice异常处理程序未启动

Java Spring Boot@ControllerAdvice异常处理程序未启动,java,spring,spring-mvc,spring-boot,kotlin,Java,Spring,Spring Mvc,Spring Boot,Kotlin,我设置了以下控制器建议,以返回错误条件的API合同: @ControllerAdvice public class ExceptionHandler : ResponseEntityExceptionHandler() { @ExceptionHandler(Throwable::class) @ResponseBody public fun onException(ex: Throwable): ResponseEntity<ErrorResponse>

我设置了以下控制器建议,以返回错误条件的API合同:

@ControllerAdvice
public class ExceptionHandler : ResponseEntityExceptionHandler()
{
    @ExceptionHandler(Throwable::class)
    @ResponseBody
    public fun onException(ex: Throwable): ResponseEntity<ErrorResponse>
    {
        val errorResponse = ErrorResponse(
           response = ResponseHeader(ex.responseCode(), ex.message))
        return ResponseEntity(errorResponse, HttpStatus.UNAUTHORIZED);
    }

}
以上是一个很好的自以为是的出发点,但现在它不会被忽视

  • 我尝试用一个
    ExceptionHandlerExceptionResolver
    实例替换错误处理程序,但没有成功
  • 我已经尝试创建自己的CustomErrorHandler,但这也不合适,因为原始异常在重新路由到自定义错误控制器时不再位于
    HttpServletRequest
    中。为了向客户机返回适当的响应,需要此信息
如何输入:

  • 使SpringBoot不将异常转发到异常控制器
  • 还原@ControllerAdvice异常处理程序,这样我就可以返回适当的响应正文和状态代码
启动spring日志时:

main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in exceptionHandler
main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in responseEntityExceptionHandler
编辑:

在阅读了Spring Boot文档之后,我了解到BasicErrorController只应在
@ControllerAdvice
未处理的任何异常情况下启动。这似乎没有发生。所以问题是为什么

我还有一个Spring安全过滤器,用于评估API密钥和访问令牌头凭证@ControllerAdvice在这里不起作用-很公平,因为我们没有处理控制器端点

使用EntryPoint和AcessDeniedHandler处理来自安全筛选器的异常。可以在内部配置以下组件:

.exceptionHandling()
如果在筛选器中扩展AbstractAuthenticationProcessingFilter,请配置FailureHandler

 setAuthenticationFailureHandler()

如果将应用程序部署到application server下,Afaik ErrorController将被覆盖。

我尝试了此操作,但没有遵循。最后我在回答中使用了这种方法。不知道这是不是一个好的。
 setAuthenticationFailureHandler()