Skip to content

Conversation

deblockt
Copy link
Contributor

On the current Spring version, usage of Jaxb2RootElementHttpMessageConverter in RestClient messageConverters don't allow to serialize JAXBElement .

This PR add this feature.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 18, 2024
@snicoll
Copy link
Member

snicoll commented Jul 19, 2024

@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?

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Jul 19, 2024
@deblockt
Copy link
Contributor Author

Hi @snicoll , This PR is the equivalent of this commit on WebClient mdeinum@9de58d7.

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.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 19, 2024
@snicoll
Copy link
Member

snicoll commented Jul 19, 2024

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.

@snicoll snicoll self-assigned this Jul 19, 2024
@snicoll snicoll added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Jul 19, 2024
@snicoll snicoll added this to the 6.2.0-M7 milestone Jul 19, 2024
@snicoll
Copy link
Member

snicoll commented Jul 19, 2024

Thanks again @deblockt!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants