在Spring Controller中获取HashMap



实际上,我正试图为我的办公室食堂制作一个android应用程序,因为那里已经有了Spring web应用程序。在安卓应用程序中,我会显示当天可用的所有项目。员工将填写相应项目的数量,然后提交。为此,我在Spring控制器中创建了一个webService,并且我能够通过REST(使用getForObject)访问android应用程序中的所有项目。我已经将id作为item_id(使用rest从远程数据库获取)提供给EditText,每当用户点击订单时,我都会创建一个HashMap,在其中放入item_id,即所有项目的订单数量。但我遇到了使用rest将HashMap发送到Spring web应用程序的问题。我尝试使用restTemplate.getForObject("http://172.16.1.2/webapp/rest/restPlaceOrder?order="+order, List.class);发送。但它是以…的形式接收的/restPlaceOrder?阶数={1=2,2=7,8=4}。正如你所说,我应该使用postForObject而不是getForObject。现在,我尝试在spring控制器中使用postForObject和@RequestBody。但现在,每当我在spring控制器中使用@RequestBody时,android restclient都会出现错误,即不支持媒体类型。所以请帮助我使用postForObject发送HashMap,并在spring控制器中捕获相同的东西。我正在使用安卓客户端中的代码作为

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);

在Spring Controller的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;
} 

使用@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;
} 

最新更新