問題:如何處理管理環境中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 –