Provide sample value for request parameter in Swagger
For Spring Boot users, assuming you've a REST method, accepting json
body, but for some reasons doesn't explicitly uses @RequestBody
. Follow below steps to generate proper Swagger documentation
Update SpringFox
configuration bean for additional model
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
// ...
.additionalModels(new TypeResolver().resolve(YourRequestModel.class));
}
Update controller API for @ApiImplicitParams
@PostMapping("/your-api")
@ApiOperation(value = "brief description", notes = "Greater details come here")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "x-locale", example = "en"),
@ApiImplicitParam(paramType = "body", dataType = "YourRequestModel")
})
public YourResponsetModel processRequest() {
// ...
return null;
}
This will generate us Swagger with an optional header x-locale
, and body
of type YourRequestModel
.
Unfortunately you cannot provide an sample or example value for atomic parametera (String, Number, ...).
You can only provide an example if the parameter is an object with a schema, you only have to add an example
property to the property description:
properties:
firstName:
description: first name
type: string
example: John
As a last resort you could add an example value in the parameter's description (value
in the ApiImplicitParam
annotation).
@ApiImplicitParam(
name = "message",
value = "Message that is sent to the method. Example: value",
required = true,
dataType = "string",
paramType = "body"
)