首先,对不起我的英语,它不是我的主要语言,我不确定这个问题是否完全可以理解。
在收到作为 Rest Web 服务的 @RequestParam 的 map 后,我需要做一些查询。
我正在尝试使用 Postman 调用 Web 服务,这里是完整的 POST 请求 http://localhost:8080/CDRestApi/rest/cd/esibizione/getIdUdFromIstanzaMetadatoByMap/5/map?25=ALAN&26=IANESELLI
这是我的 WS 代码:
@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();
}
这是 postman 的回答:
{
"timestamp": 1505834218902,
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.NullPointerException",
"message": "No message available",
"path": "/CDRestApi/rest/cd/esibizione/getIdUdFromIstanzaMetadatoByMap/5/map"
System.out 输出是:
17:16:58,891 INFO [stdout] (default task-4) null
我想 mapQuery 对象没有值(value),因为它没有被正确地赋值
我已经看过那些帖子,但它们对我没有用: Map<String, String> as @RequestParam in Spring MVC 和 Spring MVC + RequestParam as Map + get URL array parameters not working
我是否错过了正确的 Postman POST 请求?还是webservice本身的问题?
最佳答案
我解决了获取 map 并将其转换为 map 的问题。
Map<Integer, String> mappaQuery = mapQuery.entrySet().stream().collect(Collectors.toMap(e -> Integer.parseInt(e.getKey()), Map.Entry::getValue));
虽然效率不高,但可以用于 Postman 调用的测试目的;在 WS 的真正调用中,我可以传递一个适当的 Map 对象。
关于java - 如何使用 Postman 将 map<Integer, String> 作为@RequestParam 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46304362/