question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to properly annotate and present a Map type request body?

See original GitHub issue

I’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,

  1. 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.
  2. 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:closed
  • Created 7 years ago
  • Comments:36 (12 by maintainers)

github_iconTop GitHub Comments

19reactions
hello-joshcommented, Jan 25, 2017

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

@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 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.

1reaction
dilipkrishcommented, Apr 10, 2018
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found