Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jaxb2XmlEncoder can not encode JAXBElement #32972

Closed
deblockt opened this issue Jun 6, 2024 · 2 comments
Closed

Jaxb2XmlEncoder can not encode JAXBElement #32972

deblockt opened this issue Jun 6, 2024 · 2 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another

Comments

@deblockt
Copy link
Contributor

deblockt commented Jun 6, 2024

Affects: <Spring Framework version>


Spring boot: 3.3.0

I'm trying to use WebClient to send XML date using Jaxb.

I have generate my classes using jaxb2-maven-plugin it seems that this plugin don't add @XmlRootElement on classes.

Without this annotation the Jaxb marshaler don't want to serialize my data with this error impossible to serialize type “xxxxxx” as an element, as it has no @XmlRootElement annotation.

The other way to serialize data is to use JAXBElement, but the JAXBElement is not supported by Jaxb2XmlEncoder, the can encode method say:

if (super.canEncode(elementType, mimeType)) {
	Class<?> outputClass = elementType.toClass();
	return (outputClass.isAnnotationPresent(XmlRootElement.class) ||
			outputClass.isAnnotationPresent(XmlType.class));
}
else {
	return false;
}

to work properly, I have create a customer encoder with this code:

public class CustomXmlEncoder extends Jaxb2XmlEncoder {

    @Override
    public boolean canEncode(ResolvableType elementType, MimeType mimeType) {
        if (elementType.isAssignableFrom(JAXBElement.class)) {
            return true;
        }
        return super.canEncode(elementType, mimeType);
    }
}

I don't know is there is a reason to not support JAXBElement while this issue #30552 provides support on serialization process.

Thanks for your help

@deblockt deblockt changed the title Jaxb2XmlEncoder can not encode Jaxb2XmlEncoder Jaxb2XmlEncoder can not encode JAXBElement Jun 6, 2024
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 6, 2024
@deblockt
Copy link
Contributor Author

deblockt commented Jun 7, 2024

A project to reproduce the issue has been created here: spring-jaxb-error-main.zip

@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Jun 7, 2024
@sbrannen
Copy link
Member

sbrannen commented Jun 7, 2024

Hi @deblockt,

Thanks for raising the issue.

Please note, however that there is no need to create an issue and subsequently a PR for that issue.

In light of that, I am closing this issue as:

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2024
@sbrannen sbrannen added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 7, 2024
sbrannen pushed a commit that referenced this issue Jun 7, 2024
Commit d7970e4 introduced support for JAXBElement in
Jaxb2XmlEncoder's encodeValue() method; however, canEncode() still
returned false for a JAXBElement element type.

This commit revises canEncode() so that it returns true for an element
type that is assignable from JAXBElement.

See gh-30552
See gh-32972
Closes gh-32977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

3 participants