gpt4 book ai didi

java - Spring RestClient 内容类型不匹配

转载 作者:行者123 更新时间:2023-12-01 13:52:46 28 4
gpt4 key购买 nike

我正在尝试使用 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 指针也会有所帮助。

附注在 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>

最佳答案

我相信您需要设置由您的 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

关于java - Spring RestClient 内容类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19842446/

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