Skip to content

Support JAXBElement in Jaxb2RootElementHttpMessageConverter #33233

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {

@Override
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
return (AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && canWrite(mediaType));
return ((JAXBElement.class.isAssignableFrom(clazz) || AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null) && canWrite(mediaType));
}

@Override
@@ -192,7 +192,7 @@ protected Source processSource(Source source) {
@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception {
try {
Class<?> clazz = ClassUtils.getUserClass(o);
Class<?> clazz = getMarshallerType(o);
Marshaller marshaller = createMarshaller(clazz);
setCharset(headers.getContentType(), marshaller);
marshaller.marshal(o, result);
@@ -205,6 +205,15 @@ protected void writeToResult(Object o, HttpHeaders headers, Result result) throw
}
}

private static Class<?> getMarshallerType(Object o) {
if (o instanceof JAXBElement<?> jaxbElement) {
return jaxbElement.getDeclaredType();
}
else {
return ClassUtils.getUserClass(o);
}
}

private void setCharset(@Nullable MediaType contentType, Marshaller marshaller) throws PropertyException {
if (contentType != null && contentType.getCharset() != null) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharset().name());
Original file line number Diff line number Diff line change
@@ -18,6 +18,9 @@

import java.nio.charset.StandardCharsets;

import javax.xml.namespace.QName;

import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import jakarta.xml.bind.annotation.XmlAttribute;
@@ -93,6 +96,8 @@ void canWrite() {
.as("Converter does not support writing @XmlRootElement subclass").isTrue();
assertThat(converter.canWrite(rootElementCglib.getClass(), null))
.as("Converter does not support writing @XmlRootElement subclass").isTrue();
assertThat(converter.canWrite(JAXBElement.class, null))
.as("Converter does not support writing JAXBElement").isTrue();
assertThat(converter.canWrite(Type.class, null))
.as("Converter supports writing @XmlType").isFalse();
}
@@ -186,6 +191,18 @@ void writeXmlRootElement() throws Exception {
.isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>", ev);
}

@Test
void writeJaxbElementRootElement() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
JAXBElement jaxbElement = new JAXBElement<>(new QName("custom"), MyCustomElement.class, new MyCustomElement("field1", "field2"));
converter.write(jaxbElement, null, outputMessage);
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML);
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarTo("<custom><field1>field1</field1><field2>field2</field2></custom>", ev);
}

@Test
void writeXmlRootElementSubclass() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();