Search in sources :

Example 1 with JAXBException

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

the class AbstractJaxb2HttpMessageConverter method createMarshaller.

/**
 * Create a new {@link Marshaller} for the given class.
 * @param clazz the class to create the marshaller for
 * @return the {@code Marshaller}
 * @throws HttpMessageConversionException in case of JAXB errors
 */
protected final Marshaller createMarshaller(Class<?> clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        Marshaller marshaller = jaxbContext.createMarshaller();
        customizeMarshaller(marshaller);
        return marshaller;
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not create Marshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
    }
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) JAXBException(jakarta.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) JAXBContext(jakarta.xml.bind.JAXBContext)

Example 2 with JAXBException

use of jakarta.xml.bind.JAXBException 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)

Example 3 with JAXBException

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

the class AbstractJaxb2HttpMessageConverter method createUnmarshaller.

/**
 * Create a new {@link Unmarshaller} for the given class.
 * @param clazz the class to create the unmarshaller for
 * @return the {@code Unmarshaller}
 * @throws HttpMessageConversionException in case of JAXB errors
 */
protected final Unmarshaller createUnmarshaller(Class<?> clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        customizeUnmarshaller(unmarshaller);
        return unmarshaller;
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not create Unmarshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
    }
}
Also used : JAXBException(jakarta.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) JAXBContext(jakarta.xml.bind.JAXBContext) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Example 4 with JAXBException

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

the class Jaxb2XmlDecoder method unmarshal.

private Object unmarshal(List<XMLEvent> events, Class<?> outputClass) {
    try {
        Unmarshaller unmarshaller = initUnmarshaller(outputClass);
        XMLEventReader eventReader = StaxUtils.createXMLEventReader(events);
        if (outputClass.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(eventReader);
        } else {
            JAXBElement<?> jaxbElement = unmarshaller.unmarshal(eventReader, outputClass);
            return jaxbElement.getValue();
        }
    } catch (UnmarshalException ex) {
        throw new DecodingException("Could not unmarshal XML to " + outputClass, ex);
    } catch (JAXBException ex) {
        throw new CodecException("Invalid JAXB configuration", ex);
    }
}
Also used : UnmarshalException(jakarta.xml.bind.UnmarshalException) JAXBException(jakarta.xml.bind.JAXBException) XMLEventReader(javax.xml.stream.XMLEventReader) DecodingException(org.springframework.core.codec.DecodingException) CodecException(org.springframework.core.codec.CodecException) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Aggregations

JAXBException (jakarta.xml.bind.JAXBException)4 JAXBContext (jakarta.xml.bind.JAXBContext)2 Marshaller (jakarta.xml.bind.Marshaller)2 Unmarshaller (jakarta.xml.bind.Unmarshaller)2 CodecException (org.springframework.core.codec.CodecException)2 HttpMessageConversionException (org.springframework.http.converter.HttpMessageConversionException)2 MarshalException (jakarta.xml.bind.MarshalException)1 UnmarshalException (jakarta.xml.bind.UnmarshalException)1 OutputStream (java.io.OutputStream)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 DecodingException (org.springframework.core.codec.DecodingException)1 EncodingException (org.springframework.core.codec.EncodingException)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1