gpt4 book ai didi

java - 使用 JAXB 处理 REST 调用 xml 响应

转载 作者:太空宇宙 更新时间:2023-11-04 07:16:28 25 4
gpt4 key购买 nike

我已向“http://services.enterprisecloud.terremark.com/cloudapi/ecloud/organizations/ ”发送了 GET REST 调用,响应为:

HTTP/1.1 200 OK Content-Length: 1373 Content-Type: application/vnd.tmrk.cloud.organization; type=collection x-tmrk-currentuser: /cloudapi/ecloud/admin/users/101 x-tmrk-token: cloud-F2A27F74-C04B-4566-AB53-CCC06DA2F798 Date: Thu, 12 May 2011 19:09:13 GMT 
<Organizations href="/cloudapi/ecloud/organizations" type="application/vnd.tmrk.cloud.organization; type=collection" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
.....
.....

供应商提供了一个 XSD,它没有生成任何组织类型或组织类型的类。 JAXB 生成的组织相关类是 ArrayOfOrganization,如下所示:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArrayOfOrganizationType", propOrder = {
"organization"
})
public class ArrayOfOrganizationType {

@XmlElement(name = "Organization", nillable = true)
protected List<OrganizationType> organization;
....
....

当我尝试使用以下代码时:

ResponseEntity exchange = template.exchange(URL, 
HttpMethod.GET,
new HttpEntity(operation.getInput(), operation.getHeader()),
ArrayOfOrganizationType.class,
urlVariables);

我得到的错误是:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.trmk.dto.ArrayOfOrganizationType] and content type [application/vnd.tmrk.cloud.organization;type=collection]

在 spring-dispatcher.xml 中,我有以下内容:

<!-- Rest client -->
<bean id="httpClient" class="org.apache.http.impl.client.DefaultHttpClient">
<constructor-arg>
<bean class="org.apache.http.impl.conn.PoolingClientConnectionManager" />
</constructor-arg>
</bean>

<bean id="restClient" class="com.transport.ext.RestClient">
</bean>

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"
p:marshaller-ref="jaxb2Marshaller" p:unmarshaller-ref="jaxb2Marshaller"
p:supportedMediaTypes="application/vnd.tmrk.cloud.organization" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
</list>
</property>
</bean>

<bean id="jaxb2Marshaller" class="com.util.DefaultJaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>java.lang.String</value>
<value>com.trmk.dto.ArrayOfOrganizationType</value>
</list>
</property>
</bean>
<!-- End of Rest client -->

任何有关如何继续的建议都将非常有帮助,因为我已经没有办法解决这个问题了

最佳答案

您可以尝试将 @XmlRootElement(name="Organizations") 添加到 ArrayOfOrganizationType 中,但显然这并不理想,因为每次重新生成 JAXB 类时都需要重新添加它。

关于java - 使用 JAXB 处理 REST 调用 xml 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19887759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com