Skip to content

Commit 7b5e4b0

Browse files
committed
Merge pull request #33233 from deblockt
* pr/33233: Polish "Support JAXBElement in Jaxb2RootElementHttpMessageConverter" Support JAXBElement in Jaxb2RootElementHttpMessageConverter Closes gh-33233
2 parents e4edd32 + f4b2886 commit 7b5e4b0

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
121121

122122
@Override
123123
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
124-
return (AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && canWrite(mediaType));
124+
boolean supportedType = (JAXBElement.class.isAssignableFrom(clazz) ||
125+
AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null);
126+
return (supportedType && canWrite(mediaType));
125127
}
126128

127129
@Override
@@ -190,12 +192,12 @@ protected Source processSource(Source source) {
190192
}
191193

192194
@Override
193-
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception {
195+
protected void writeToResult(Object value, HttpHeaders headers, Result result) throws Exception {
194196
try {
195-
Class<?> clazz = ClassUtils.getUserClass(o);
197+
Class<?> clazz = getMarshallerType(value);
196198
Marshaller marshaller = createMarshaller(clazz);
197199
setCharset(headers.getContentType(), marshaller);
198-
marshaller.marshal(o, result);
200+
marshaller.marshal(value, result);
199201
}
200202
catch (MarshalException ex) {
201203
throw ex;
@@ -205,6 +207,15 @@ protected void writeToResult(Object o, HttpHeaders headers, Result result) throw
205207
}
206208
}
207209

210+
private static Class<?> getMarshallerType(Object value) {
211+
if (value instanceof JAXBElement<?> jaxbElement) {
212+
return jaxbElement.getDeclaredType();
213+
}
214+
else {
215+
return ClassUtils.getUserClass(value);
216+
}
217+
}
218+
208219
private void setCharset(@Nullable MediaType contentType, Marshaller marshaller) throws PropertyException {
209220
if (contentType != null && contentType.getCharset() != null) {
210221
marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharset().name());

spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import java.nio.charset.StandardCharsets;
2020

21+
import javax.xml.namespace.QName;
22+
23+
import jakarta.xml.bind.JAXBElement;
2124
import jakarta.xml.bind.Marshaller;
2225
import jakarta.xml.bind.Unmarshaller;
2326
import jakarta.xml.bind.annotation.XmlAttribute;
@@ -93,6 +96,8 @@ void canWrite() {
9396
.as("Converter does not support writing @XmlRootElement subclass").isTrue();
9497
assertThat(converter.canWrite(rootElementCglib.getClass(), null))
9598
.as("Converter does not support writing @XmlRootElement subclass").isTrue();
99+
assertThat(converter.canWrite(JAXBElement.class, null))
100+
.as("Converter does not support writing JAXBElement").isTrue();
96101
assertThat(converter.canWrite(Type.class, null))
97102
.as("Converter supports writing @XmlType").isFalse();
98103
}
@@ -186,6 +191,18 @@ void writeXmlRootElement() throws Exception {
186191
.isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>", ev);
187192
}
188193

194+
@Test
195+
void writeJaxbElementRootElement() throws Exception {
196+
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
197+
JAXBElement jaxbElement = new JAXBElement<>(new QName("custom"), MyCustomElement.class, new MyCustomElement("field1", "field2"));
198+
converter.write(jaxbElement, null, outputMessage);
199+
assertThat(outputMessage.getHeaders().getContentType())
200+
.as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML);
201+
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
202+
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
203+
.isSimilarTo("<custom><field1>field1</field1><field2>field2</field2></custom>", ev);
204+
}
205+
189206
@Test
190207
void writeXmlRootElementSubclass() throws Exception {
191208
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();

0 commit comments

Comments
 (0)