问题:如何处理管理环境中MVC端点引发的异常?Spring Boot Actuator Management端点异常处理
public class MyMvcEndpoints extends EndpointMvcAdapter {
@ResponseBody
@RequestMapping(method = GET, value = "/foo", produces = APPLICATION_JSON_VALUE)
public Foo foo() {
throw new FooNotFound("Foo Not Found!");
}
}
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class FooNotFound extends RuntimeException {
public NotFoundException(String message){
super(message);
}
}
在当前的配置,我接收500
代替404
。我尝试了通常的方法来处理异常,如ControllerAdvice,ExceptionHandler方法等。没有成功。
我还发现CompositeHandlerExceptionResolver
在EndpointWebMvcChildContextConfiguration
这是负责异常处理。我试图定义自己的HandlerExceptionResolver
bean,但它在管理上下文中找不到我的解析器。
这看起来应该按原样工作。一些Spring Boot自己的端点在异常时使用'@ ResponseStatus'。 [这是一个例子](https://github.com/spring-projects/spring-boot/blob/52536dc291da1b3417f465d73b72d60011b53fda/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ EnvironmentMvcEndpoint.java#L122)。对于你没有展示的东西,一定还有更多的东西。 –
有趣的是,在发布问题之前,我已经测试了您发送的确切代码以查看它的工作原理:它还返回了'500'。 – Rad
@Rad你能找到任何解决方案吗?我有完全相同的问题。使用Spring引导1.5.6 –