Skip to content

Add support for @ExceptionHandler inside @RestController #748

Closed
@wtell400

Description

@wtell400

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

Activity

changed the title [-]@RestController @ExceptionHandler not generated in OpenAPI[/-] [+]Add support for @ExceptionHandler inside @RestController[/+] on Jun 26, 2020
bnasslahsen

bnasslahsen commented on Jun 26, 2020

@bnasslahsen
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bnasslahsen@wtell400

        Issue actions

          Add support for @ExceptionHandler inside @RestController · Issue #748 · springdoc/springdoc-openapi