处理REST使用JAXB调用xml响应

时间:2013-11-10 08:37:54

标签: java xml spring rest jaxb

我已向“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 -->

任何有关如何继续进行的建议都会非常有用,因为我已经用尽了解决这个问题的想法

1 个答案:

答案 0 :(得分:1)

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