Skip to content

Add support for @ExceptionHandler inside @RestController #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wtell400 opened this issue Jun 24, 2020 · 1 comment
Closed

Add support for @ExceptionHandler inside @RestController #748

wtell400 opened this issue Jun 24, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@wtell400
Copy link

When creating an @ExceptionHandler in a @RestController, similar to @RestControllerAdvice I expect the OpenAPI documentation to contain the response for this ExceptionHandler. I'm not sure if this is a bug of a missing feature and thus a feature request.

@RestControllerAdvice
public class ExceptionControllerAdvice
{
    // This is generated in OpenAPI documentation
    @ExceptionHandler(CustomException1.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorMessage handleException1(RuntimeException e)
    {
        return new ErrorMessage(HttpStatus.NOT_FOUND, e.getMessage());
    }
}
@RestController
public class ClassExceptionHandlerController
{
    // This is not generated in OpenAPI documentation
    @ExceptionHandler(CustomException2.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorMessage handleException(RuntimeException e)
    {
        return new ErrorMessage(HttpStatus.BAD_REQUEST, e.getMessage());
    }

    @GetMapping("/classexceptionhandler/1")
    public void test1() {
        throw new CustomException1();
    }

    @GetMapping("/classexceptionhandler/2")
    public void test2() {
        throw new CustomException2();
    }
}

Expected:

  • Both 400 (from @RestController) and 404 (from @RestControllerAdvice) as responses of mappings.

Actual:

  • Only 404 (from @RestControllerAdvice) as response of mappings.

Using springdoc-openapi 1.4.1. See attached project.
springdoc-bug-classexceptionhandler.zip

@bnasslahsen bnasslahsen changed the title @RestController @ExceptionHandler not generated in OpenAPI Add support for @ExceptionHandler inside @RestController Jun 26, 2020
@bnasslahsen
Copy link
Collaborator

@wtell400,

The support is now added on master, and you can validate it with the latest SNAPHSOT;
It will be available in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants