How to properly annotate and present a Map type request body?
See original GitHub issueI’m using springfox-swagger2
2.2.2 with Spring Boot.
One of my REST APIs takes a Map<String, Object>
object in request body, like this,
@ApiOperation(
value = "Submit user data")
@RequestMapping(
value = "/userData",
method = RequestMethod.POST,
consumes = {MediaType.APPLICATION_JSON_VALUE},
produces = "application/json")
public ResponseEntity<?> submitUserData(
@RequestBody
@ApiParam(
value = "JSON format input, keys allowed are key1, key2, key3."
) Map<String, Object> userInput
I want to present the users a sample or the userInput
, by default it only shows Model Schema
as {}
, I’d like to see something like,
{
"key1":"sample value1",
"key2":"sample value2",
}
The generated Swagger JSON is as follows,
{
"in": "body",
"name": "userInput",
"description": "JSON format input, keys allowed are key1, key2, key3.",
"required": false,
"schema": {
"type": "object"
}
}
So here are my main questions based on my understanding,
- Looks like if I can manage to add
additionalProperties
into the generated JSON, I should be able to get what I want, a key list for the users to follow. But I’m really not sure how to achieve this with annotations. - Seems there is another key called
example
that can gets me what I want, but not sure how can I use it with Springfox-Swagger.
I think the bottom line is I’m using a Map for request body type instead of having a dedicated class for it, thus I cannot utilize @ApiModel
and @ApiModelProperties
to customize the documents. Or can I?
Issue Analytics
- State:
- Created 7 years ago
- Comments:36 (12 by maintainers)
Top Results From Across the Web
Document a @RequestBody Map in Swagger - Stack Overflow
So OpenAPI does provide parameter input of type query to support that . While stating the type ... @RequestBody Map<String, Object> request.
Read more >Spring's RequestBody and ResponseBody Annotations
In this quick tutorial, we provide a concise overview of the Spring @RequestBody and @ResponseBody annotations.
Read more >Spring MVC @RequestMapping Annotation Example with ...
We can use @RequestMapping with @RequestParam annotation to retrieve the URL parameter and map it to the method argument. For example:
Read more >Using the Spring @RequestMapping Annotation
@RequestMapping annotation provides a header element to narrow down the request mapping based on headers present in the request. You can specify ...
Read more >Spring Data REST Reference Guide
The POST method creates a new entity from the given request body. ... Flagged with the @Projection annotation and located in the same ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is the exact use-case I am trying to solve for: http://stackoverflow.com/questions/41861164/how-can-i-manually-describe-an-example-input-for-a-java-requestbody-mapstring
I couldn’t care less what the user send in, my api is just storing user defined data. What I do care about is being able to document it in swagger ui with some sort of example data, but
@ExampleProperty
doesn’t seem to work as I understood it from the docs.@apoorvatarento like this