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

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

    Issue actions