【问题标题】:Spring restclient content type mismatchSpring restclient 内容类型不匹配
【发布时间】:2013-11-07 17:20:26
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

    标签: java xml spring rest


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 2015-06-06
      • 2015-05-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      相关资源
      最近更新 更多