如何插入地图<整数,字符串>作为@RequestParam与邮差

问题描述:

首先对不起我的英语,这不是我的主要语言,我不知道这个问题是完全可以理解的。如何插入地图<整数,字符串>作为@RequestParam与邮差

我需要接受Map作为一个REST Web服务的@RequestParam后做一些查询。

我试图与邮差来调用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(); 
} 

这是邮递员回答:

{ 
"timestamp": 1505834218902, 
"status": 500, 
"error": "Internal Server Error", 
"exception": "java.lang.NullPointerException", 
"message": "No message available", 
"path": "/CDRestApi/rest/cd/esibizione/getIdUdFromIstanzaMetadatoByMap/5/map" 

}

在S ystem.out打印是:

17:16:58,891 INFO [stdout] (default task-4) null 

我猜想mapQuery对象没有价值,因为它没有正确增值的

我已经看到了这些帖子,但他们不是对我很有用: Map<String, String> as @RequestParam in Spring MVCSpring MVC + RequestParam as Map + get URL array parameters not working

难道我错过了正确的邮差POST请求?或者它是web服务本身的问题?

+0

你有什么错误? – Plog

+0

我会加入问题 – Fjordo

+0

如果你做一个Map 和String key =“25”;而不是'Integer key = 25;''它会起作用吗? – Plog

我解决了这个让地图,并将其转换成地图。

Map<Integer, String> mappaQuery = mapQuery.entrySet().stream().collect(Collectors.toMap(e -> Integer.parseInt(e.getKey()), Map.Entry::getValue)); 

效率不高,但它的工作对邮差通话的测试目的;在WS的真正调用中,我可以传递适当的Map对象。