Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring GlobalExceptionHandler : java.lang.IllegalStateException: Could not resolve parameter [0] ... No suitable resolver

Tags:

spring

I had the following error when trying to setup a global exception handler to respond with a generic error response:

@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")
    }
}

and the error:

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)

This is not a duplicate of spring mvc controller error java.lang.IllegalStateException: No suitable resolver for argument [0] which was an Hibernate issue. I am not using Hibernate here.

like image 268
pyb Avatar asked Mar 22 '19 21:03

pyb


People also ask

How do you handle a spring boot exception?

Exception Handler Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown. This method should be used for writing the Controller Advice class file. Now, use the code given below to throw the exception from the API.

How do you handle exceptions in spring boot Microservices?

Exception Handling in Spring Boot helps to deal with errors and exceptions present in APIs so as to deliver a robust enterprise application. This article covers various ways in which exceptions can be handled in a Spring Boot Project. Let's do the initial setup to explore each approach in more depth.

What is ControllerAdvice in spring?

@ControllerAdvice is a specialization of the @Component annotation which allows to handle exceptions across the whole application in one global handling component. It can be viewed as an interceptor of exceptions thrown by methods annotated with @RequestMapping and similar.


1 Answers

In my case, this was a copy-paste error.

I was throwing a RuntimeException but I configured the exception handler to support HttpClientErrorException :

fun handleServerException(exception: HttpClientErrorException)

In this case, the fix is to use the same Exception class in the @ExceptionHandler annotation as in the method parameter:

@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)
}
like image 105
pyb Avatar answered Nov 11 '22 14:11

pyb