On the current Spring version, usage of Jaxb2RootElementHttpMessageConverter in RestClient messageConverters don't allow to serialize JAXBElement.
This PR add this feature.
Comment From: snicoll
@deblockt thanks for the PR but it looks a little strange to me. Shouldn't the conversion happen via the content-type rather than via supporting such a type? Where would you be using this exactly?
Comment From: deblockt
Hi @snicoll , This PR is the equivalent of this commit on WebClient https://github.com/mdeinum/spring-framework/commit/9de58d78da431a4a94d08cab964221b62a3c527b.
In my project, I try to serialize XML object using RestClient with this code.
restClient = RestClient.builder()
.messageConverters((httpMessageConverters -> {
httpMessageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}))
.build();
restClient
.post().uri("/")
.contentType(MediaType.APPLICATION_XML)
.body(new ObjectFactory().createPojo(pojo))
.retrieve()
.body(MyPojo.class);
Where new ObjectFactory().createPojo(pojo) return a JAXBElement<MyPojo>.
The ObjectFactory and MyPojo classes are generated using jaxb2-maven-plugin and this plugin don't annotate classes with @XmlRootElement so we should use the JAXBElement to generate a valid XML.
Comment From: snicoll
Thanks for the additional details.
The ObjectFactory and MyPojo classes are generated using jaxb2-maven-plugin and this plugin don't annotate classes with @XmlRootElement so we should use the JAXBElement to generate a valid XML.
Yeah I didn't mean that @XmlRootElement was mandatory but rather that the content type should drive the conversion, not the type to serialize but I can see how that's short sighted. And the fact we've done this for codecs means we should do it for MVC as well.
Comment From: snicoll
Thanks again @deblockt!