gpt4 book ai didi

java - 休息模板。从八位字节流中解码 JaxB 对象

转载 作者:行者123 更新时间:2023-11-29 04:47:28 26 4
gpt4 key购买 nike

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

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

requestresponse 是 JAXB 对象。这是 Spring 配置:

<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 修复?

更新

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

关于java - 休息模板。从八位字节流中解码 JaxB 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36618555/

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