【问题标题】:Get HashMap in Spring Controller在 Spring Controller 中获取 HashMap
【发布时间】:2016-06-13 07:59:58
【问题描述】:

实际上,我正在尝试为我的办公室食堂制作一个 Android 应用程序,因为 Spring Web 应用程序已经存在。在 Android 应用程序中,我显示了当天可用的所有项目。员工将填写相应项目的数量并提交。为此,我在 Spring 控制器中创建了一个 webService,并且能够通过 REST(使用 getForObject)访问 android 应用程序中的所有项目。我已将 ID 作为 item_id 提供给 EditText(使用 rest 从远程数据库中获取),并且每当用户单击下订单时,我都会在其中创建一个 HashMap,我将 item_id 放入所有项目的订单数量。但是我面临着使用 rest 将该 HashMap 发送到 Spring Web 应用程序的问题。我尝试使用 restTemplate.getForObject("http://172.16.1.2/webapp/rest/restPlaceOrder?order="+order, List.class); 发送。但它以 .../restPlaceOrder?order={1=2,2=7,8=4} 的形式接收。正如你所说,我应该使用 postForObject 而不是 getForObject。现在我尝试使用 postForObject 并在 spring 控制器中使用 @RequestBody 。但是现在每当我在 spring 控制器中使用 @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);

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

          } 

【问题讨论】:

  • 您真的希望您的 URL 看起来像 .../restPlaceOrder?order={1=2,2=7,8=4} 吗?您不应该依赖 HashMap 的 toString() 来设计您的 URL。这张地图应该是什么?你的方法被命名为placeOrder()。因此,我假设它是为了创造 东西,而不是得到东西。所以它应该使用 POST,而不是 GET。
  • 是的,JB,绝对正确。我想用这个订单图(整数,整数)做的是将这对(item_id,qty)存储到数据库中,但我不知道如何从rest webservice调用到spring contrloler获取这张图。
  • 正如我所说:使用 POST 而不是 GET,将该 Map 作为请求的主体而不是作为查询参数发送,并通过使用 @RequestBody 注释参数在服务器上获取它而不是@RequestParam。并且由于您期望 Map,请将其设为方法参数的类型,而不是 Object。
  • 但是每当我将 HashMap 放入像 postForObject(url, null, String.class, order) 这样的请求正文中并使用 @RequestBody HashMap 或者它在 android 客户端中显示不受支持的媒体类型错误
  • 在您的问题中详细说明该问题。告诉你在做什么,发布代码,发布异常的完整堆栈跟踪。安卓跟这个有什么关系?你有一个 Spring 服务器和一个 Spring 客户端。

标签: spring rest controller hashmap


【解决方案1】:

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

      } 

【讨论】:

    猜你喜欢
    • 2012-05-25
    • 2022-01-19
    • 1970-01-01
    • 2013-07-29
    • 2020-05-30
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    相关资源
    最近更新 更多