Search in sources :

Example 1 with MarshalException

use of jakarta.xml.bind.MarshalException in project spring-framework by spring-projects.

the class Jaxb2XmlEncoder method encodeValue.

@Override
public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
    if (!Hints.isLoggingSuppressed(hints)) {
        LogFormatUtils.traceDebug(logger, traceOn -> {
            String formatted = LogFormatUtils.formatValue(value, !traceOn);
            return Hints.getLogPrefix(hints) + "Encoding [" + formatted + "]";
        });
    }
    boolean release = true;
    DataBuffer buffer = bufferFactory.allocateBuffer(1024);
    try {
        OutputStream outputStream = buffer.asOutputStream();
        Class<?> clazz = ClassUtils.getUserClass(value);
        Marshaller marshaller = initMarshaller(clazz);
        marshaller.marshal(value, outputStream);
        release = false;
        return buffer;
    } catch (MarshalException ex) {
        throw new EncodingException("Could not marshal " + value.getClass() + " to XML", ex);
    } catch (JAXBException ex) {
        throw new CodecException("Invalid JAXB configuration", ex);
    } finally {
        if (release) {
            DataBufferUtils.release(buffer);
        }
    }
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) MarshalException(jakarta.xml.bind.MarshalException) EncodingException(org.springframework.core.codec.EncodingException) OutputStream(java.io.OutputStream) JAXBException(jakarta.xml.bind.JAXBException) CodecException(org.springframework.core.codec.CodecException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

JAXBException (jakarta.xml.bind.JAXBException)1 MarshalException (jakarta.xml.bind.MarshalException)1 Marshaller (jakarta.xml.bind.Marshaller)1 OutputStream (java.io.OutputStream)1 CodecException (org.springframework.core.codec.CodecException)1 EncodingException (org.springframework.core.codec.EncodingException)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1