2016-10-08 75 views
1

我试图通过邮递员来调用这个API:无法通过地图RESTful方法

@POST 
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
public void printDetails(final MultivaluedMap<String, String> formParams) { 

    for(String key : formParams.keySet()) { 
     System.out.println(key + "  " + formParams.get(key)); 
    } 
} 

但地图原来是空的。请帮助我一样。

PS:这是我第一次尝试将可变数量的参数传递给api。我提到了 sending List/Map as POST parameter jerseyHow to access parameters in a RESTful POST method

我想我错就错在我传递参数邮递员方式:postman image

请帮我一样。也请帮助如何通过ajax(在JS)调用这个API。

+0

我想知道的答案是否是有帮助的办呢?请让我知道,如果它不是,并尝试此解决方案后得到什么,以便我可以看看它并解决。 – notionquest

+0

@notionquest对不起,回复晚了。我刚刚试过你的答案。不幸的是,它也给了我一张空的地图! –

+0

您是否仍面临同样的问题或设法找到解决方案? – notionquest

回答

1

我发现了一个可能的答案。

@POST 
public void printDetails() { 
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); 
    Map<String, String[]> mapp = request.getParameterMap(); 
    for(String key : mapp.keySet()) { 
     System.out.println(key + "  " + mapp.get(key)[0]); 
    } 
} 

仍然不知道如何通过传递“最后MultivaluedMap”的论点

1

设置请求标头为"application/x-www-form-urlencoded"

Postman request header

请求体 - 选择生提供值如下所述: -

{ 
    "LOCATION": "Singapore" 
} 

enter image description here