Support JAXBElement in Jaxb2RootElementHttpMessageConverter
See gh-33233
This commit is contained in:
parent
e4edd3246a
commit
a5e2557738
|
|
@ -121,7 +121,7 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
|
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
|
@Override
|
||||||
|
|
@ -192,7 +192,7 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
|
||||||
@Override
|
@Override
|
||||||
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception {
|
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception {
|
||||||
try {
|
try {
|
||||||
Class<?> clazz = ClassUtils.getUserClass(o);
|
Class<?> clazz = getMarshallerType(o);
|
||||||
Marshaller marshaller = createMarshaller(clazz);
|
Marshaller marshaller = createMarshaller(clazz);
|
||||||
setCharset(headers.getContentType(), marshaller);
|
setCharset(headers.getContentType(), marshaller);
|
||||||
marshaller.marshal(o, result);
|
marshaller.marshal(o, result);
|
||||||
|
|
@ -205,6 +205,15 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
private void setCharset(@Nullable MediaType contentType, Marshaller marshaller) throws PropertyException {
|
||||||
if (contentType != null && contentType.getCharset() != null) {
|
if (contentType != null && contentType.getCharset() != null) {
|
||||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharset().name());
|
marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharset().name());
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,9 @@ package org.springframework.http.converter.xml;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import javax.xml.namespace.QName;
|
||||||
|
|
||||||
|
import jakarta.xml.bind.JAXBElement;
|
||||||
import jakarta.xml.bind.Marshaller;
|
import jakarta.xml.bind.Marshaller;
|
||||||
import jakarta.xml.bind.Unmarshaller;
|
import jakarta.xml.bind.Unmarshaller;
|
||||||
import jakarta.xml.bind.annotation.XmlAttribute;
|
import jakarta.xml.bind.annotation.XmlAttribute;
|
||||||
|
|
@ -93,6 +96,8 @@ class Jaxb2RootElementHttpMessageConverterTests {
|
||||||
.as("Converter does not support writing @XmlRootElement subclass").isTrue();
|
.as("Converter does not support writing @XmlRootElement subclass").isTrue();
|
||||||
assertThat(converter.canWrite(rootElementCglib.getClass(), null))
|
assertThat(converter.canWrite(rootElementCglib.getClass(), null))
|
||||||
.as("Converter does not support writing @XmlRootElement subclass").isTrue();
|
.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))
|
assertThat(converter.canWrite(Type.class, null))
|
||||||
.as("Converter supports writing @XmlType").isFalse();
|
.as("Converter supports writing @XmlType").isFalse();
|
||||||
}
|
}
|
||||||
|
|
@ -186,6 +191,18 @@ class Jaxb2RootElementHttpMessageConverterTests {
|
||||||
.isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>", ev);
|
.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
|
@Test
|
||||||
void writeXmlRootElementSubclass() throws Exception {
|
void writeXmlRootElementSubclass() throws Exception {
|
||||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue