我需要将请求发送到服务并将响应解析为jaxb对象。这是服务呼叫代码:

        ProviderResponse response = restTemplate.postForObject(
            URL,
            request,
            ProviderResponse.class
        );


requestresponse是JAXB对象。这是弹簧配置:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
            <property name="marshaller" ref="jaxbMarshaller"/>
            <property name="unmarshaller" ref="jaxbMarshaller"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>


<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.service.data.ProviderRequest</value>
            <value>com.service.data.ProviderResponse</value>
        </list>
    </property>
</bean>


运行此代码时,出现异常:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.service.data.ProviderResponse] and content type [application/octet-stream]


我试图这样做:

        String responseStr = restTemplate.postForObject(
            URL,
            request,
            String.class
        );

        ProviderResponse response = stringToJaxb(responseStr, ProviderResponse.class);


而且效果很好,但我认为这不是一个好的解决方案。
也许可以通过spring config来解决?

UPD

ProviderResponse类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "header",
    "body"
})
@XmlRootElement(name = "providerResponse")
public class ProviderResponse {

    @XmlElement(required = true)
    protected String header;
    @XmlElement(required = true)
    protected ProviderResponse.Body body;


    public String getHeader() {
        return header;
    }


    public void setHeader(String value) {
        this.header = value;
    }


    public ProviderResponse.Body getBody() {
        return body;
    }


    public void setBody(ProviderResponse.Body value) {
        this.body = value;
    }


    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "code",
        "serviceStatus"
    })
    public static class Body {

        @XmlElement(required = true)
        protected String code;
        protected String serviceStatus;


        public String getCode() {
            return code;
        }

        public void setCode(String value) {
            this.code = value;
        }

        public String getServiceStatus() {
            return serviceStatus;
        }


        public void setServiceStatus(String value) {
            this.serviceStatus = value;
        }

    }

}

最佳答案

对于RestTemplate,它不使用在应用程序上下文中配置的转换器。您必须创建一个HttpMessageConverters列表,并在RestTemplate构造函数中提供它,如下所示

MarshallingHttpMessageConverter marshallingConverter = new   MarshallingHttpMessageConverter();
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(ProviderRequest.class ,ProviderResponse.class);
marshallingConverter.setMarshaller(marshaller);
marshallingConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM , MediaType.APPLICATION_XML , MediaType.TEXT_XML));
RestTemplate restTemplate = new RestTemplate(Collections.singletonList(converter));

08-05 00:20