Spring RestTemplate内容类型不匹配

f45qwnt8  于 2023-09-29  发布在  Spring
关注(0)|答案(1)|浏览(137)

我正在尝试使用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.在 Spring 调度员中,我有以下内容:

<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>
mftmpeh8

mftmpeh81#

我相信你需要设置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

相关问题