It seems that there is a bug in SwaggerUI because as soon as I set the mediaType of @ExampleProperty
to application/json
the example value is empty as you can see here:
I've tried several ways but none is working. According to this it seems to be a popular problem: https://github.com/springfox/springfox/issuesW/2352
I've tried an unlogical solution (does not work):
@ApiOperation(
value = "Sends a request to the AiController to predict the request",
produces = "application/json",
consumes = "application/json",
authorizations = @Authorization(value = "Bearer"))
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example(
value = {@ExampleProperty(mediaType = "application/json", value = "test")}))
Another one (also not working):
@ApiOperation(
value = "Sends a request to the AiController to predict the request",
produces = "application/json",
consumes = "application/json",
authorizations = @Authorization(value = "Bearer"))
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example(
value = {
@ExampleProperty(
mediaType = "application/json",
value = "{\"code\" : \"42\", \"message\" : \"Invalid ID supplied\"}")
}))
A simpler version (not working):
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example(
value = {
@ExampleProperty(
value = "{'property': 'test'}",
mediaType = "application/json")
}))
But as soon that I change the mediaType to don't have a specific type it works:
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "Successfully retrieved predictions",
examples =
@Example({
@ExampleProperty(
mediaType = "*/*",
value = "{\n\"predictions\": [ \n \"cat\" \n]\n}")
}))
Output is:
{
"predictions": [
"cat"
]
}
Thats nearly what I want to have but of course the indentation is wrong.
Is there any other way to do an @ApiResponse
example? Can't I give an example with my DTO (?):
@ApiModel(
value = "Cat or Dog response",
description = "Response of the prediction whether it's a cat or dog")
@Data
public class CatOrDogResponse {
@ApiModelProperty(value = "Images to predict", example = "test", required = true)
private String[] predictions;
}
My used Spring Fox versions:
compile group: 'io.springfox', name: 'springfox-swagger2', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-data-rest', version: '3.0.0'
compile group: 'io.springfox', name: 'springfox-bean-validators', version: '3.0.0'
In the end the answer is easy. I found this on GitHub: https://github.com/springfox/springfox/issues/2538#issuecomment-637265748
The bug should be fixed but it's still not fixed. To get the example value working for responses the annotation of the REST type needs to be adjusted:
@PostMapping(value = "/predict", produces = "application/json", consumes = "application/json")
I give you a concrete example with my methods:
@ApiOperation(value = "Sends a request to the AiController to predict the request", response =
CatOrDogResponse.class, authorizations = @Authorization(value = "Bearer"))
@ApiResponses(value = {@ApiResponse(code = 200, message = "Successfully retrieved predictions"),
@ApiResponse(code = 401, message = "You are not authorized to send a request"), @ApiResponse(code = 403,
message = "Accessing the resource you were trying to reach is forbidden"), @ApiResponse(code = 404,
message = "The resource you were trying to reach is not found")})
@PostMapping(value = "/predict", produces = "application/json", consumes = "application/json")
public Mono<CatOrDogResponse> predict(@RequestHeader HttpHeaders headers, @ApiParam(name = "Cat or Dog Prediction "
+ "Request", value = "Parameters for predicting whether an image or multiple images is/are a cat or dog") @Valid @RequestBody CatOrDogRequest catOrDogRequest) {
...
}
Important is that you define the response class in the @ApiOperation
like me: response = CatOrDogResponse.class
In the response class you define the example value for the response:
@ApiModel(value = "Cat or Dog response", description = "Response of the prediction whether it's a cat or dog")
@Data
public class CatOrDogResponse {
@ApiModelProperty(value = "Images to predict", example = "cat")
private String[] predictions;
}
What changed now is that finally automatically the response type is declared as application/json
instead of */*
. The correct indentation works automatically. And as you can see I'm not using produces
and consumes
in the @ApiOperation
anymore because it's useless.