1

我正在使用 spring 的 resttemplate 来调用 rest api 获取错误以解组 xml 以对象我的代码是:-

String  uri = "http://devd.webservice.com/devpl/api/1.0/credential?apiKey=" + apiKey + "&signature=" + signature + "&timeStamp=" + timeStamp;
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("accountName", accountName);
        requestHeaders.set("containerName", containerName);
        requestHeaders.set("folderPath", folderPath);
        requestHeaders.set("Content-Type","application/xml");
        requestHeaders.set("Accept","application/xml");
        RestTemplate template = getRestTemplate();
        HttpEntity<String> requestEntity = new HttpEntity<String>(requestHeaders);
        Credential result =(Credential)template.postForObject(uri,requestEntity,Credential.class);

我使用对象的对象类 bean:-

   package com.simplidrivechn.netmagicsolutions.bean;
    import com.thoughtworks.xstream.annotations.*;

    @XStreamAlias("credential")
    public class Credential
    {
    private String DestinationUrl;
    private String AuthToken;
    private String StorageUrl;

        public String getAuthToken() {
            return AuthToken;
        }

        public void setAuthToken(String AuthToken) {
            this.AuthToken = AuthToken;
        }

        public String getDestinationUrl() {
            return DestinationUrl;
        }

        public void setDestinationUrl(String DestinationUrl) {
            this.DestinationUrl = DestinationUrl;
        }

        public String getStorageUrl() {
            return StorageUrl;
        }

        public void setStorageUrl(String StorageUrl) {
            this.StorageUrl = StorageUrl;
        }
      }

我的弹簧配置文件: -

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
    <list>
      <bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <property name="marshaller" ref="xstreamMarshaller" />
        <property name="unmarshaller" ref="xstreamMarshaller" />
      </bean>
    </list>
    </property>
  </bean>

 <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
  <property name="aliases">
    <props>
      <prop key="credential">com.simplidrivechn.netmagicsolutions.bean.Credential</prop>
    </props>
  </property>
 </bean>
</beans>

我收到错误:-

Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read [class com.simplidrivechn.netmagicsolutions.bean.Credential]; nested exception is org.springframework.oxm.UnmarshallingFailureException: XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.mapper.CannotResolveClassException: Credential : Credential

请帮我解决这个错误

4

2 回答 2

1

定义别名的另一种方法是自动扫描。

<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
    <property name="autodetectAnnotations" value="true"/>
    <property name="annotatedClasses">
        <array>
            <value>com.simplidrivechn.netmagicsolutions.bean.Credential</value>
        </array>
    </property>
</bean>
于 2013-07-01T10:14:09.813 回答
1

查看您的异常,XStreamMarshaller's您的 spring 上下文中似乎没有正确设置别名。您必须确保您的键是“别名”,即您的情况下的根元素。“凭据”是您尝试反序列化的 xml 响应的根元素吗?请注意,别名区分大小写。以下代码对我有用。注意aliases.put("person", Person.class);我的 xml 响应具有根元素“人”。如果我将此键更改为让我们说“人”,aliases.put("Person", Person.class);我会像你一样得到例外。

XStreamMarshaller marshaller = new XStreamMarshaller();
Map<String, Class> aliases = new HashMap<String, Class>();
aliases.put("person", Person.class);
marshaller.setAliases(aliases);
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(
                marshaller, marshaller);
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(converter);
template.setMessageConverters(converters);
HttpEntity request = new HttpEntity(null, headers);
ResponseEntity<Person> response = template.exchange(url,
                HttpMethod.GET, request, Person.class);
于 2013-02-18T02:10:05.883 回答