【问题标题】:Spring GlobalExceptionHandler : java.lang.IllegalStateException: Could not resolve parameter [0] ... No suitable resolverSpring GlobalExceptionHandler : java.lang.IllegalStateException: 无法解析参数 [0] ...没有合适的解析器
【发布时间】: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


【解决方案1】:

就我而言,这是一个复制粘贴错误。

我抛出了一个RuntimeException,但我将异常处理程序配置为支持HttpClientErrorException

fun handleServerException(exception: HttpClientErrorException)

在这种情况下,解决方法是在 @ExceptionHandler 注释中使用与方法参数中相同的 Exception 类:

@ExceptionHandler(Exception::class) // <-- must match method parameter
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
fun handleServerException(exception: Exception): ErrorDto { // <-- fix here
    // do some other thing with server errors, like alerts
    return ErrorDto(errorMessage)
}

【讨论】:

  • 恭喜 3K
  • 这也发生在我身上,还要确保你导入正确的ServerHttpRequestclass。就我而言,我正在导入org.springframework.http.server.ServerHttpRequest,而它应该是org.springframework.http.server.reactive.ServerHttpRequest。注意它在 reactive 包中。这与您使用 spring-boot-starter-webspring-boot-starter-webflux 的项目有关
猜你喜欢
  • 2012-10-31
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-25
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多