Spring RestTemplate不接受JAXBElement [英] Spring RestTemplate not accepting JAXBElement
问题描述
我正在尝试使用RestTemplate对服务进行简单的POST.我拥有的XSD不会生成根元素",而是具有根元素类型".但是ObjectFactory.createFoo(FooType)给了我我要发布的JAXBElement,但由于以下异常未能这样做:
I am trying to make a simple POST using RestTemplate to a service. The XSD that I have, does not generate the Root Element but instead has the Root Element Type. But ObjectFactory.createFoo(FooType) gives me the JAXBElement which I am trying to post but failing to do so with below exception:
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.foo.FooType] and content type [application/xml]
这是我的代码,调用其余服务
This is my code which calls the rest service
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
HttpEntity<FooType> entity = new HttpEntity<FooType>(request.getValue(),headers);
JAXBElement<ResponseType> response = restTemplate.postForObject(esbReplaceNumberListURI, entity, JAXBElement.class);
在通过StackOverflow中的答案后,我尝试将setSupportJaxbElementClass设置为true的Jaxb2Marshaller(来自Spring O/X jar)添加到RestTemplate中.那没有帮助.
I tried adding Jaxb2Marshaller(of Spring O/X jar ) to RestTemplate with setSupportJaxbElementClass set as true after going through an answer in StackOverflow. That did not help.
我正在如下构建我的请求对象,这给了我JAXBElement
I am building my request object as below which gives me JAXBElement
JAXBElement<FooType> request = ObjectFactory.createFoo(FooType);
然后在发布时执行如下的request.getValue():
and then while posting I do a request.getValue() as below:
HttpEntity<FooType> entity = new HttpEntity<FooType>(request.getValue(),headers);
我的Spring Config如下:
My Spring Config as below:
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(getMarshallingHttpMessageConverter());
return restTemplate;
}
@Bean
public MarshallingHttpMessageConverter getMarshallingHttpMessageConverter() {
MarshallingHttpMessageConverter marshallingConverter = new MarshallingHttpMessageConverter();
marshallingConverter.setMarshaller(jaxb2Marshaller());
marshallingConverter.setUnmarshaller(jaxb2Marshaller());
marshallingConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_XML, MediaType.TEXT_XML));
return marshallingConverter;
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setSupportJaxbElementClass(Boolean.TRUE);
marshaller.setPackagesToScan("com.foo.domain.*");
return marshaller;
}
使用Spring-Boot 1.3.5.RELEASE,而后者又使用Spring 4.2.6.RELEASE. 我竭尽全力弄清楚了这一点.非常感谢您的帮助.
Using Spring-Boot 1.3.5.RELEASE which in turn uses Spring 4.2.6.RELEASE. I am at my wits end in figuring this out. Any help is much appreciated.
推荐答案
我大约75%的人确定您应该拥有marshaller.setPackagesToScan("com.foo.domain");
,而不是marshaller.setPackagesToScan("com.foo.domain.*");
.请注意,缺少*
-我相信它正在尝试查找字面名称为*
的软件包,该软件包显然不存在.
I'm about 75% sure that you should have marshaller.setPackagesToScan("com.foo.domain");
, not marshaller.setPackagesToScan("com.foo.domain.*");
. Note the absence of the *
- I believe that it's attempting to find a package whose name literally is *
, which obviously doesn't exist.
来源:
Jaxb2Marshaller#setPackagesToScan
:
这是基于Spring的搜索,因此类似于Spring的组件扫描功能
({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner})
这表明它可能与春季参考书,其中说:
That suggests it's probably the same as described in the Spring Reference, which says:
以下是使用XML的替代方法
The following is an alternative using XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.example"/>
</beans>
有<context:component-scan base-package="org.example"/>
.
这篇关于Spring RestTemplate不接受JAXBElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!