I am designing an api where one of the POST methods that takes a Map<String, String>
of any key value pairs.
@RequestMapping(value = "/start", method = RequestMethod.POST)
public void startProcess(
@ApiParam(examples = @Example(value = {
@ExampleProperty(
mediaType="application/json",
value = "{\"userId\":\"1234\",\"userName\":\"JoshJ\"}"
)
}))
@RequestBody(required = false) Map<String, String> fields) {
// .. does stuff
}
I would like to provide an example input for fields
but I can't seem to get it to render in the swagger output. Is this not the correct way to use @Example
?
Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.
By using @RequestBody annotation you will get your values mapped with the model you created in your system for handling any specific call. While by using @ResponseBody you can send anything back to the place from where the request was generated. Both things will be mapped easily without writing any custom parser etc.
The @RequestBody annotation is applicable to handler methods of Spring controllers. This annotation indicates that Spring should deserialize a request body into an object. This object is passed as a handler method parameter.
The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the HTTP response automatically.
The issues mentioned in @g00glen00b's answer seems to be fixed. Here is a code snippet of how it can be done.
In your controller class:
// omitted other annotations
@ApiImplicitParams(
@ApiImplicitParam(
name = "body",
dataType = "ApplicationProperties",
examples = @Example(
@ExampleProperty(
mediaType = "application/json",
value = "{\"applicationName\":\"application-name\"}"
)
)
)
)
public Application updateApplicationName(
@RequestBody Map<String, String> body
) {
// ...
}
// Helper class for Swagger documentation - see http://springfox.github.io/springfox/docs/snapshot/#q27
public static class ApplicationProperties {
private String applicationName;
public String getApplicationName() {
return applicationName;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
}
Additionally, you need to add the following line to your Swagger config:
// omitted other imports...
import com.fasterxml.classmate.TypeResolver;
@Bean
public Docket api(TypeResolver resolver) {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
// the following line is important!
.additionalModels(resolver.resolve(DashboardController.ApplicationProperties.class));
}
Further documentation can be found here: http://springfox.github.io/springfox/docs/snapshot/#q27
Swagger only provides the API, these annotations still have to be integrated into the Springfox framework to work. At the time this question was posted, neither @ExampleProperty
nor @Example
were supported by Springfox. This can be seen in #853 and #1536.
Since version 2.9.0, this has been implemented. For an example, you can check this answer.
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