1

REST Web サービスを使用した Spring MVC の完全なアイデアを得るために、サンプル アプリケーションを作成しました。Web サービスをホストするアプリケーションと、この Web サービスを呼び出して関連データを取得するクライアントを作成しました。Stringのようにクライアント側から引数を渡すことができ、データをListまたは単一のオブジェクトとして受け取ることができ、ここまではスムーズです..

ここで、クライアント側からリストを引数として渡し、クライアント アプリケーションから渡されるリストを取得するために Web サービス側に実装したいと考えています。このシナリオを手伝ってくれる人はいますか?

私の作業バージョンのコード スニペットを見つけてください。

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

関連ファイルもあるのですが、コードスニペットを全部入れると余計になってしまいます。主に私のクエリは、クライアント側からリストを渡す方法と、レシーバー/サーバー側からリストを取得する方法です。どちらの側でもスプリングのみを使用しています

お時間とご協力いただきありがとうございます。

-ロナック。

4

1 に答える 1

0

CustomerBean の配列を使用する

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

配列からリストへの変換は、関心のある読者の演習として残されています...

于 2012-08-24T18:09:47.463 に答える