Search code examples
spring-bootswagger-uispring-restcontroller

For java Controller swagger-ui.html renders unexpected http codes


I have the following java endpoint inside a Springboot RestController annotated with some Swagger annotations for 4 ApiResponses:

    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully sign in"), 
            @ApiResponse(code = 400, message = "Missing request body"), 
            @ApiResponse(code = 404, message = "Schema not found"), 
            @ApiResponse(code = 500, message = "Internal error")
            })
    @PostMapping(
            path = "/login",
            produces = "application/json; charset=utf-8")
    public LoginResponse login(
            @ApiParam(
                    name="cred", 
                    value="Credenciales de quien intenta ingresar al sistema")
            @RequestBody CredencialesRequest cred
            ) throws ControllerException {
        return accessService.login(cred.getUsuario(), cred.getClave());
    }

As you can see, I have declared 4 response codes as a possible HTTP responses: 200, 400, 404 and 500

When I run the application and go to http://localhost:8080/swagger-ui.html the UI shows the 4 codes that I have described in the endpoint. However, it shows MORE http codes. Please take a look at this picture:

Extra Codes that the UI is showing

The extra codes are: 201 (created), 401 (unauthorized) & 403 (forbidden). Why? For my use case, the "login" endpoint should be always accessible to any user, so at least, 401 & 403 doesn't make sense at all, in this context.


Solution

  • Just like it was said in the comments, in order to remove the extra http codes in the swagger UI, we need to modify our configuration file by adding useDefaultResponseMessages(false) to the api() method in our SwaggerConfig like this:

        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .useDefaultResponseMessages(false)  // I HAD TO ADD THIS LINE !!!!
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build()
                    .apiInfo(apiInfo());
        }
    
    

    That's it !