【问题标题】:Spring Web - Restful Webservice - pass/receive ArrayList as an argument/parameter in client/server sideSpring Web - Restful Webservice - 在客户端/服务器端传递/接收 ArrayList 作为参数/参数
【发布时间】:2012-05-16 09:28:44
【问题描述】:

我创建了一个示例应用程序来全面了解带有 REST Web 服务的 Spring MVC。我创建了一个托管 Web 服务的应用程序和一个调用该 Web 服务并获取相关数据的客户端。我能够像字符串一样从客户端传递参数,并且能够以列表或单个对象的形式接收数据,直到这里一切顺利..

现在我想将列表作为参数从客户端传递,并且还想在 web 服务端实现以获取从客户端应用程序传递的列表。任何人都可以帮助解决这种情况吗?

请找到我的工作版本的代码 sn-p。

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("appContext.xml", Client.class);
RestTemplate restTemplate = applicationContext.getBean("restTemplate", RestTemplate.class);
String url;
// retrieve a list of customers
url = "http://localhost:8080/restful-ws/app/testlist.xml";

List<CustomerBean> custList = (List) restTemplate.getForObject(url, List.class);
for (CustomerBean cust : custList) {
  System.out.println(">> cust :"+ cust.toString());}

Web 服务端实现。

@RequestMapping(method=RequestMethod.GET, value="/testlist")
public ModelAndView showCustomers() {
    ModelAndView mv = new ModelAndView("customerListKey");
    List<Customer> custs = new ArrayList<Customer>();
    for (Customer customer:customers.values()) {
        custs.add(customer);
    }
    mv.addObject("allCustomers", custs);
    return mv;
}

我也有相关文件,但是如果把所有的代码sn-ps都放了,那就太多了。主要是我的查询是如何从客户端传递列表以及如何从接收器/服务器端获取它?,在双方我只使用弹簧

提前感谢您的时间和帮助。

-罗纳克。

【问题讨论】:

  • Spring MVC jsp list of objects 的可能重复项
  • @krock,我看到了你建议的链接,但它与这个不匹配。
  • @Nilesh,我的问题是,使用 restTemplate 我能够通过 URL 传递参数,如 String/Int,并且在服务器端,我能够得到它。但是我们如何使用 restTemplate 传递列表,而在服务器端我们如何检索相同的列表呢?顺便说一句,感谢您的时间和帮助!

标签: web-services spring rest arraylist resttemplate


【解决方案1】:

使用 CustomerBean 数组

CustomerBean[] custList = restTemplate.getForObject(url, CustomerBean[].class);

从数组到列表的转换留给感兴趣的读者作为练习......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2016-05-18
    • 1970-01-01
    相关资源
    最近更新 更多