S. Pauk
S. Pauk

Reputation: 5318

Swagger generates response body on 204 (No Content)

Here's a delete endpoint:

@ApiOperation(value = "delete subscription")
@ApiResponses(value = {
                @ApiResponse(code = 204, message = "Deleted", response = Void.class),
                @ApiResponse(code = 404, message = "Not Found")
})
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public ListenableFuture<ResponseEntity<Void>> delete(@PathVariable long id)

Springfox generates an API spec with a response body:

{
  "cancelled": true,
  "done": true
}

Obviously I want the response body spec to be blank for 204. How to achieve this?

N.B. if I change the endpoint be synchronous:

public void delete(@PathVariable long id)

then the generated API is fine.

Upvotes: 4

Views: 4427

Answers (1)

Perl99
Perl99

Reputation: 143

Try adding

.genericModelSubstitutes(ListenableFuture.class)

to your Docket configuration to support ListenableFuture<T>.

Docs: http://springfox.github.io/springfox/javadoc/2.7.0/springfox/documentation/spring/web/plugins/Docket.html#genericModelSubstitutes(java.lang.Class...)

Substitutes each generic class with it's direct parameterized type. genericModelSubstitutes(ResponseEntity.class) would substitute ResponseEntity<MyModel> with MyModel

For example:

return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .genericModelSubstitutes(ListenableFuture.class);

Upvotes: 1

Related Questions