Skip to content

Commit d7970e4

Browse files
committed
Support JAXBElement in Jaxb2XmlEncoder
This commit introduces support for JAXBElements in the Jaxb2XmlEncoder. Closes gh-30552
1 parent 2f78b42 commit d7970e4

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222
import java.util.function.Function;
2323

24+
import jakarta.xml.bind.JAXBElement;
2425
import jakarta.xml.bind.JAXBException;
2526
import jakarta.xml.bind.MarshalException;
2627
import jakarta.xml.bind.Marshaller;
@@ -121,7 +122,7 @@ public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
121122
DataBuffer buffer = bufferFactory.allocateBuffer(1024);
122123
try {
123124
OutputStream outputStream = buffer.asOutputStream();
124-
Class<?> clazz = ClassUtils.getUserClass(value);
125+
Class<?> clazz = getMarshallerType(value);
125126
Marshaller marshaller = initMarshaller(clazz);
126127
marshaller.marshal(value, outputStream);
127128
release = false;
@@ -140,6 +141,15 @@ public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
140141
}
141142
}
142143

144+
private static Class<?> getMarshallerType(Object value) {
145+
if (value instanceof JAXBElement<?> jaxbElement) {
146+
return jaxbElement.getDeclaredType();
147+
}
148+
else {
149+
return ClassUtils.getUserClass(value);
150+
}
151+
}
152+
143153
private Marshaller initMarshaller(Class<?> clazz) throws CodecException, JAXBException {
144154
Marshaller marshaller = this.jaxbContexts.createMarshaller(clazz);
145155
marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());

spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlEncoderTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.List;
2121
import java.util.function.Consumer;
2222

23+
import javax.xml.namespace.QName;
24+
25+
import jakarta.xml.bind.JAXBElement;
2326
import jakarta.xml.bind.annotation.XmlElement;
2427
import jakarta.xml.bind.annotation.XmlElements;
2528
import jakarta.xml.bind.annotation.XmlRootElement;
@@ -76,6 +79,18 @@ public void encode() {
7679
.verifyComplete());
7780
}
7881

82+
@Test
83+
public void encodeJaxbElement() {
84+
Mono<JAXBElement<Pojo>> input = Mono.just(new JAXBElement<>(new QName("baz"), Pojo.class,
85+
new Pojo("foofoo", "barbar")));
86+
87+
testEncode(input, Pojo.class, step -> step
88+
.consumeNextWith(expectXml(
89+
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" +
90+
"<baz><bar>barbar</bar><foo>foofoo</foo></baz>"))
91+
.verifyComplete());
92+
}
93+
7994
@Test
8095
public void encodeError() {
8196
Flux<Pojo> input = Flux.error(RuntimeException::new);

0 commit comments

Comments
 (0)