Skip to content

Support for adding additional models for request or response that are not inferred from operations #735

@cabbonizio

Description

@cabbonizio

Hi, this is just a question about how to configure a the ModelRef object within the ResponseMessage object. Essentially, I would like to default the ResponseMessages globally within startup of Sprint Boot application such as HTTP 400, 404, and 500. I did figure out how to do this which is to use the
globalResponseMessage() method of the Docket object like such:

@bean
public Docket swaggerApi() {
return new Docket(DocumentationType.SWAGGER_2).select().paths(userOnlyEndpoints()).build()
.pathMapping("/rest/").globalResponseMessage(RequestMethod.GET, getDefaultResponseMessages()).apiInfo(apiInfo());
}

private List getDefaultResponseMessages() {
List defaultResponseMessages = Lists.newArrayList();
defaultResponseMessages.add(new ResponseMessage(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), new ModelRef("")));
defaultResponseMessages.add(new ResponseMessage(HttpStatus.NOT_FOUND.value(), HttpStatus.NOT_FOUND.getReasonPhrase(), new ModelRef("")));
defaultResponseMessages.add(new ResponseMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), new ModelRef("")));
return defaultResponseMessages;
}

It is working defaulting each of the GET methods in my controller with those HTTP status codes, but I want to be able to specify an "error" response object which is different than the response if the return code was a 200. Where I'm getting hung up is how to configure ModelRef() to specify the error object (Error.class). I can actually do this within each controller method using Swaggers @ApiResponses({ @apiresponse(code = 404, message = "Not Found", response=Error.class) }) but I would prefer to do this in my Spring Boot config class (Application.java) and just trying to determine how do this with the ResponseMessage object.

Any help would be appreciated thanks very much. This is by far one of the best and easiest ways to incorporate Swagger into a Spring (especially Boot) application. Our developers are all having fun using this.

Chris

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions