我正在使用resttemplate of spring来调用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
请帮我解决此错误
答案 0 :(得分:1)
查看您的异常,看起来在您的spring上下文中未正确设置XStreamMarshaller's
别名。您必须确保您的密钥是“别名”,即您的案例中的根元素。 “凭证”是您尝试反序列化的xml响应的根元素吗?请注意,别名区分大小写。以下代码适合我。注意aliases.put("person", Person.class);
我的xml响应具有根元素'person'。如果我改变这个键,让我们像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);
答案 1 :(得分: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>