I am using springfox-swagger2 version 2.6.1, and it is inserting HTTP 200 response messages for PUT and POST operations automatically, despite my attempts to configure it not to do so (I do not use response status 200 for POST or PUT, but 201 and 204, respectively); see below screenshot:
I have seen answers to similar questions where the authors suggest adding a @ResponseStatus
annotation to your controller to "fix" it, but this becomes inflexible and goes against Spring's own documentation regarding the use of ResponseEntity
vs @ResponseStatus
for rest APIs. Examples:
How to change the response status code for successful operation in Swagger?
and
https://github.com/springfox/springfox/issues/908
Is there any other way to force Springfox Swagger not to add this 200 OK status code?
My Docket configuration:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.select().
apis(RequestHandlerSelectors.any()).
paths(paths()).
build()
.pathMapping("/")
.apiInfo(apiInfo())
.genericModelSubstitutes(ResponseEntity.class)
.alternateTypeRules(newRule(
typeResolver.resolve(DeferredResult.class, typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
typeResolver.resolve(WildcardType.class)
));
...and the actual API endpoint declaration:
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
@ApiOperation(value = "Create a new enrolment", code = 201)
@ApiResponses(value = {
@ApiResponse(code = 201, message = "New enrolment created",
responseHeaders = @ResponseHeader(name = "Location", description = "The resulting URI of the newly-created enrolment", response = String.class))})
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<Void> saveNewEnrolment(@ApiParam(value = "Enrolment to save", required = true) @RequestBody final Enrolment enrolment) {
// implementation code removed; "location" header is created and returned
return ResponseEntity.created(location).build();
}
Try adding @ResponseStatus(HttpStatus.CREATED)
or @ResponseStatus(HttpStatus.NO_CONTENT)
annotation. Taken from here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With