简体   繁体   中英

Sending map in postman post request

I can't find a good answer as to how to format a map in my json post when I want it to map directly to my Java pojo with the @RequestBody annotation. I'm assuming the json would look something like:

{
    "myInt":"10",
    "myMap":"{1:\"A\"}"
}

My pojo would have a myInt field and a myMap field. The myMap field is of type Map<Integer,String>

What does the json for the map look like to get this to work?

According to your JSON structure myMap is a String . However, even if you remove the quotes from the value of myMap you will find that {1:"A"} is not valid JSON, valid JSON syntax requires that all property keys are strings. A valid JSON structure would look like {"1":"A"} . The deserializer should be able to coerce the key into an Integer , so Map<Integer, String> is fine.

First, make sure to have something like the following resource method:

@Path("/url")
public class Test {

    @POST
 @Consumes(MediaType.APPLICATION_JSON)
    public Response post(@RequestBody Foo foo) {
        ...
    }
}

Then, when you send the request through POSTMAN, select the type as POST, then select the "raw" option and then just send a JSON in the "body" with the values you want to put in your Map. Remember to select "application/json" . Jackson will transform the JSON into a Map for you.

{
   "myInt": 10,
   "myMap": {
         1: "A"
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM