在Spring控制器中获取HashMap
实际上,我正在尝试为我的办公室食堂制作一个android应用程序,因为SpringWeb应用程序已经存在。在Android应用程序中,我显示了那天所有可用的项目。员工将填写相应项目的数量,然后提交。为此,我在SpringController中创建了一个webService,我可以通过REST访问android应用程序中的所有项目(使用在Spring控制器中获取HashMap,spring,rest,controller,hashmap,Spring,Rest,Controller,Hashmap,实际上,我正在尝试为我的办公室食堂制作一个android应用程序,因为SpringWeb应用程序已经存在。在Android应用程序中,我显示了那天所有可用的项目。员工将填写相应项目的数量,然后提交。为此,我在SpringController中创建了一个webService,我可以通过REST访问android应用程序中的所有项目(使用getForObject)。我已经给EditText指定了id作为item_id(使用rest从远程数据库获取),每当用户单击PlaceOrder时,我都会创建一个
getForObject
)。我已经给EditText指定了id作为item_id(使用rest从远程数据库获取),每当用户单击PlaceOrder时,我都会创建一个HashMap,在其中我为所有项目放置item_id、order qty。但我面临着使用rest将HashMap发送到SpringWeb应用程序的问题。我尝试使用restemplate.getForObject(“http://172.16.1.2/webapp/rest/restPlaceOrder?order=“+顺序、列表、类别)代码>。但它是以…/resplaceorder?order={1=2,2=7,8=4}的形式接收的。正如你所说,我应该使用postForObject而不是getForObject。现在我尝试在spring控制器中使用postForObject和@RequestBody。但现在每当我在spring controller中使用@RequestBody时,android restclient就会给出一个错误,不支持的媒体类型。因此,请帮助我使用postForObject发送HashMap,并在spring控制器中捕获相同的内容。我使用android客户端中的代码作为
HashMap<Integer, Integer> order=new HashMap<Integer, Integer>();
order.put(1, 5); order.put(2, 4);
String url="http://172.16.1.2/webapp/rest/restPlaceOrder"
RestTemplate restTemplate1=new RestTemplate();
restTemplate1.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
List<LinkedHashMap> res=restTemplate1.postForObject(url, null, List.class, order);
HashMap order=newhashmap();
命令.付诸表决(1,5);命令.付诸表决(2,4);
字符串url=”http://172.16.1.2/webapp/rest/restPlaceOrder"
RestTemplate restTemplate1=新的RestTemplate();
restTemplate1.getMessageConverters().add(新映射Jackson2HttpMessageConverter());
List res=restemplate1.postForObject(url,null,List.class,order);
在SpringController中的web应用程序中,我使用
@RequestMapping(value="/restPlaceOrder", method= {RequestMethod.GET, RequestMethod.POST}, headers="Accept=application/json")
public @ResponseBody List<Schedule> placeOrder(@RequestBody HashMap<Integer, Integer> order, HttpServletRequest request, HttpServletResponse response, Model m){
System.out.println("welcome taking request");
/*problem is whenever i put @RequestBody, Restclient shows unsupported media type and if I remove @RequestBody, then it works but then how to get that HashMap here */
List<Schedule> sc=new ArrayList();
return sc;
}
@RequestMapping(value=“/resplaceorder”,method={RequestMethod.GET,RequestMethod.POST},headers=“Accept=application/json”)
public@ResponseBody List placeOrder(@RequestBody HashMap order,HttpServletRequest,HttpServletResponse,Model m){
System.out.println(“欢迎接受请求”);
/*问题是,每当我放置@RequestBody时,Restclient都会显示不支持的媒体类型,如果我删除@RequestBody,它就会工作,但如何在这里获取HashMap*/
List sc=new ArrayList();
返回sc;
}
使用@RequestParam
@RequestMapping(value="/restPlaceOrder", method= {RequestMethod.POST})
public @ResponseBody List<Schedule> placeOrder(@RequestParam HashMap<Integer, Integer> order, HttpServletRequest request, HttpServletResponse response, Model m){
System.out.println("welcome taking request");
List<Schedule> sc=new ArrayList();
return sc;
}
@RequestMapping(value=“/resplaceorder”,method={RequestMethod.POST})
public@ResponseBody List placeOrder(@RequestParam HashMap order,HttpServletRequest,HttpServletResponse,Model m){
System.out.println(“欢迎接受请求”);
List sc=new ArrayList();
返回sc;
}
您真的希望您的URL看起来像../resplaceorder?order={1=2,2=7,8=4}
?您不应该依赖HashMap的toString()来设计URL。这张地图应该是什么?您的方法名为placeOrder()
。因此,我认为这是为了创造一些东西,而不是得到一些东西。所以它应该使用帖子,而不是GET。是的,JB,绝对正确。我想用这个订单映射(Integer,Integer)将这对(item_id,qty)存储到数据库中,但我不知道如何从rest webservice调用spring Controller中获取这个映射。正如我所说的:使用POST而不是get,将该映射作为请求体而不是查询参数发送,并通过使用@RequestBody
而不是@RequestParam
注释参数在服务器上获取它。由于您需要一个映射,所以请将该方法参数的类型设置为thah对象。但是,每当我将HashMap放入请求体中,如postForObject(url、null、String.class、order)并使用@RequestBody HashMap时,或者它在android客户端中显示未被抑制的媒体类型错误时,请在您的问题中说明该问题。告诉你正在做什么,发布代码,发布异常的完整堆栈跟踪。android与此有什么关系?您有一个Spring服务器和一个Spring客户端。