RestTemplate。从octet-stream解组JaxB对象

时间:2016-04-14 09:11:30

标签: java spring jaxb

我需要向服务发送请求并将响应解析为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;
        }

    }

}

2 个答案:

答案 0 :(得分:2)

对于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));

答案 1 :(得分:1)

在MarshallingHttpMessageConverter上设置支持的媒体类型,如下所示

<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"/>
            <property name="supportedMediaTypes">
               <list>
                  <value>application/xml</value>
                  <value>text/xml</value>
                  <value>application/octet-stream</value>
               </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>