1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
import java .util .Map ;
22
22
import java .util .function .Function ;
23
23
24
+ import jakarta .xml .bind .JAXBElement ;
24
25
import jakarta .xml .bind .JAXBException ;
25
26
import jakarta .xml .bind .MarshalException ;
26
27
import jakarta .xml .bind .Marshaller ;
@@ -121,7 +122,7 @@ public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
121
122
DataBuffer buffer = bufferFactory .allocateBuffer (1024 );
122
123
try {
123
124
OutputStream outputStream = buffer .asOutputStream ();
124
- Class <?> clazz = ClassUtils . getUserClass (value );
125
+ Class <?> clazz = getMarshallerType (value );
125
126
Marshaller marshaller = initMarshaller (clazz );
126
127
marshaller .marshal (value , outputStream );
127
128
release = false ;
@@ -140,6 +141,15 @@ public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
140
141
}
141
142
}
142
143
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
+
143
153
private Marshaller initMarshaller (Class <?> clazz ) throws CodecException , JAXBException {
144
154
Marshaller marshaller = this .jaxbContexts .createMarshaller (clazz );
145
155
marshaller .setProperty (Marshaller .JAXB_ENCODING , StandardCharsets .UTF_8 .name ());
0 commit comments