Search code examples
springspring-bootswaggerswagger-uiswagger-codegen

useDefaultResponseMessages(false) not working


I'm using the swagger-codegen to create a spring-server.

I also used the .useDefaultResponseMessages(false)-attribute as described in Swagger - Springfox always generates some response messages (401,403...) by default. How can I remove them?

SwaggerConfig.java:

public Docket customImplementation() {
return new Docket(DocumentationType.SWAGGER_2)
    .useDefaultResponseMessages(false)
    .select()
    .apis(RequestHandlerSelectors.basePackage("myrest.api"))
    .build()
    .directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class)
    .directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class)
    .apiInfo(apiInfo());}

related apipart: Api.java:

    @ApiOperation(value = "", notes = "Returns all clouds from the system that the user has access to ", response = Cloud.class, responseContainer = "List", tags = {
      "cloud",})
  @ApiResponses(value = {
      @ApiResponse(code = 200, message = "All clouds ", response = Cloud.class),
        /*@ApiResponse(code = 401, message = "Authorization for this action is missing", response = Error.class),
        @ApiResponse(code = 403, message = "Forbidden action", response = Error.class),
        @ApiResponse(code = 500, message = "An unexpected Error occured", response = Error.class),*/
      @ApiResponse(code = 504, message = "Server temporary not available", response = Error.class)})

  @RequestMapping(value = "/clouds",
      produces = {"application/json"},
      method = RequestMethod.GET)
  ResponseEntity<List<Cloud>> findClouds();

But the swagger-ui still looks like: swagger-ui: ResponseMessageTable

So it seems .useDefaultResponseMessages(false) is not working.

How do I disable these default error responses?


@John Duskin

I changes the Docketinitialization,changed the @Controller to @Restcontroller but I still get the 404 Message by Get

different looking 404-Message

The generated Serverstubs from Swagger-Codegen looks like:

Api.java:

@Api(value = "clouds", description = "the clouds API")
public interface CloudsApi {

@ApiOperation(value = "", notes = "Returns all clouds from the system that the user has access to ", response = Cloud.class, responseContainer = "List", tags={ "cloud", })
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "All clouds ", response = Cloud.class),
        @ApiResponse(code = 401, message = "Authorization for this action is missing", response = Cloud.class),
        @ApiResponse(code = 403, message = "Forbidden action", response = Cloud.class),
        @ApiResponse(code = 500, message = "An unexpected Error occured", response = Cloud.class),
        @ApiResponse(code = 504, message = "Server temporary not available", response = Cloud.class) })

@RequestMapping(value = "/clouds",
        produces = { "application/json" },
        method = RequestMethod.GET)
ResponseEntity<List<Cloud>> findClouds();

and the separated Controller:

@RestController
public class CloudsApiController implements CloudsApi {

@Autowired
private UserService userService;
@Autowired
private CloudService cloudService;

public ResponseEntity<List<Cloud>> findClouds() {
//do some magic
    return new ResponseEntity<List<Cloud>>(cloudList, HttpStatus.OK);
}
[...]
}

Solution

  • fixed the problem beside working ...

    this was a Spring Annotation Problem. In my SwaggerDocumentationConfig.java I added @EnableSwagger2 and everything works as wanted

    @Configuration
    @EnableSwagger2
    public class SwaggerDocumentationConfig {
    
    @Bean
    public Docket customImplementation(){
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .select()
                    .apis(RequestHandlerSelectors.basePackage("myrest.api"))
                    .build()
                .directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class)
                .directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class)
                .apiInfo(apiInfo());
        }
    }