【发布时间】:2019-03-22 21:30:47
【问题描述】:
在尝试设置全局异常处理程序以使用通用错误响应进行响应时出现以下错误:
@RestControllerAdvice
class GlobalExceptionHandler {
@ExceptionHandler(HttpClientErrorException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun handleClientException(exception: HttpClientErrorException): ErrorDto {
// do something with client errors, like logging
return ErrorDto(errorMessage)
}
@ExceptionHandler(Exception::class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
fun handleServerException(exception: HttpClientErrorException): ErrorDto {
// do some other thing with server errors, like alerts
return ErrorDto(errorMessage)
}
}
data class ErrorDto(val message: String)
@RestController
class DemoController {
@GetMapping("/error")
@ResponseBody
fun error(): ErrorDto {
throw RuntimeException("test")
}
}
和错误:
ExceptionHandlerExceptionResolver : Failure in @ExceptionHandler
public ErrorDto
GlobalExceptionHandler.handleServerException(org.springframework.web.client.HttpClientErrorException)
java.lang.IllegalStateException: Could not resolve parameter [0] in
public ErrorDto
GlobalExceptionHandler.handleServerException(org.springframework.web.client.HttpClientErrorException):
No suitable resolver at
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:163)
at
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at
(...)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
这不是 spring mvc controller error java.lang.IllegalStateException: No suitable resolver for argument [0] 的重复,这是一个 Hibernate 问题。我这里没有使用 Hibernate。
【问题讨论】:
-
为了任何来自 Google 的人的利益,我遇到了这个问题,并将其追溯到我的“处理程序”方法中引发的第二个异常。
Failure in @ExceptionHandler在处理程序捕获错误时发生,然后在方法中抛出 不同 异常
标签: spring