8

Spring RestTemplate次のような従業員レコードのリストを取得するために使用しようとしています。

public List<Employee> getEmployeesByFirstName(String firstName) {   
return restTemplate.getForObject(employeeServiceUrl + "/firstname/{firstName}", List.class, firstName);
}

問題は、Web サービス (呼び出されている) が次の XML 形式を返すことです。

<従業員> <従業員> .... </従業員> <従業員> .... </従業員> </従業員>

したがって、上記の方法を実行すると、次のエラーが発生します。

org.springframework.http.converter.HttpMessageNotReadableException: Could not read [interface java.util.List]; nested exception is org.springframework.oxm.UnmarshallingFailureException: XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.mapper.CannotResolveClassException: **employees : employees**
4

4 に答える 4

16

あなたはおそらく次のようなものを探しています:

public List<Employee> getEmployeeList() {
  Employee[] list = restTemplate.getForObject("<some URI>", Employee[].class);
  return Arrays.asList(list);
}

自動マーシャリングを使用して、正しくマーシャリングする必要があります。

于 2012-02-21T10:50:10.480 に答える
1

パラメータをRestTemplateコンストラクタに渡すMarshallerとUnmarshallerにdefaultImplementationが設定されていることを確認してください。

例:

XStreamMarshaller marshaller = new XStreamMarshaller();
marshaller.getXStream().addDefaultImplementation(ArrayList.class,List.class);

XStreamMarshaller unmarshaller = new XStreamMarshaller();
unmarshaller.getXStream().addDefaultImplementation(ArrayList.class,List.class);

RestTemplate template = new RestTemplate(marshaller, unmarshaller);
于 2012-08-23T11:59:27.613 に答える
0

RestTemplate を RestClient として使用しようとしていましたが、次のコードはリストを取得するために機能します。

public void testFindAllEmployees() {
    Employee[] list = restTemplate.getForObject(REST_SERVICE_URL, Employee[].class);
    List<Employee> elist = Arrays.asList(list);
    for(Employee e : elist){
        Assert.assertNotNull(e);
    }
}

Domain オブジェクトに適切に注釈が付けられ、クラスパスに XMLStream jar があることを確認してください。上記の条件を満たして動作する必要があります。

于 2012-12-11T19:42:59.727 に答える
0

同様の問題があり、次の例のように解決しました。

http://blog.springsource.com/2009/03/27/rest-in-spring-3-resttemplate/

于 2011-10-07T16:51:39.040 に答える