Skip to content

Jaxb2XmlEncoder can not encode JAXBElement #32972

@deblockt

Description

@deblockt

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: supersededAn issue that has been superseded by another

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions