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.
2121import java .util .Map ;
2222import java .util .function .Function ;
2323
24+ import jakarta .xml .bind .JAXBElement ;
2425import jakarta .xml .bind .JAXBException ;
2526import jakarta .xml .bind .MarshalException ;
2627import 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 ());
0 commit comments