Spring restclient内容类型不匹配

时间:2013-11-07 17:20:26

标签: java xml spring rest

我正在尝试使用Terremark企业云中的一个REST Web服务。这是我做的: 1)获取xsd并生成jaxb工件 2)发送Rest调用并让Restclient填充Organizations类。

ResponseEntity exchange = template.exchange("https://services.enterprisecloud.terremark.com/cloudapi/ecloud/organizations/", 
                    HttpMethod.GET, 
                    new HttpEntity(operation.getInput(), operation.getHeader()), 
                    Organizations.class, 
                    urlVariables);

我看到的错误是

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

在上面的错误中,com.dto.Organizations是由JAXB生成的java类。任何解决此问题的通用Spring指针也会有所帮助。

P.S。 在春季调度员中,我有以下内容:

<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" />
            <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>

1 个答案:

答案 0 :(得分:1)

我认为您需要设置MarshallingHttpMessageConverter处理的内容类型:

...
<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"/>
...

......沿着这些方向发展。 MarshallingHttpMessageConverter默认支持的媒体类型只是application/*+xml