Reputation: 872
first of all, sorry for my english, it's not my main language, and I'm not sure the question is fully understandable.
I need to do some queries after receiving a Map as a @RequestParam of a Rest Web Service.
I'm trying to call the web service with Postman, and here is the full POST request http://localhost:8080/CDRestApi/rest/cd/esibizione/getIdUdFromIstanzaMetadatoByMap/5/map?25=ALAN&26=IANESELLI
This is my WS code:
@RequestMapping(value = { "/getIdUdFromIstanzaMetadatoByMap/{id}/map" }, method = RequestMethod.POST, produces = {
MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public String selectIstanzaMetadato(@PathVariable Long id,
@RequestParam (value="map", required=true) Map<Integer,String> mapQuery) {
Integer key = 25;
System.out.println(mapQuery.get(key));
return mapQuery.get(key).toString();
}
And this is the postman answer:
{
"timestamp": 1505834218902,
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.NullPointerException",
"message": "No message available",
"path": "/CDRestApi/rest/cd/esibizione/getIdUdFromIstanzaMetadatoByMap/5/map"
}
the System.out print is:
17:16:58,891 INFO [stdout] (default task-4) null
I suppose the mapQuery object has no value, because it is not correctly valorized
I have already seen those posts, but they were not useful to me: Map<String, String> as @RequestParam in Spring MVC and Spring MVC + RequestParam as Map + get URL array parameters not working
Do I miss the correct Postman POST request ? or it is a problem of the webservice itself ?
Upvotes: 2
Views: 2179
Reputation: 872
I resolved this getting the map and converting it into a map.
Map<Integer, String> mappaQuery = mapQuery.entrySet().stream().collect(Collectors.toMap(e -> Integer.parseInt(e.getKey()), Map.Entry::getValue));
It is not efficient, but it worked for the test purposes of Postman call; in the real call of the WS, I can pass a proper Map object.
Upvotes: 1