Search in sources :

Example 6 with HttpMessageConversionException

use of org.springframework.http.converter.HttpMessageConversionException 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) throws JAXBException {
    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(javax.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 7 with HttpMessageConversionException

use of org.springframework.http.converter.HttpMessageConversionException in project spring-framework by spring-projects.

the class AbstractJaxb2HttpMessageConverter method getJaxbContext.

/**
	 * Return a {@link JAXBContext} for the given class.
	 * @param clazz the class to return the context for
	 * @return the {@code JAXBContext}
	 * @throws HttpMessageConversionException in case of JAXB errors
	 */
protected final JAXBContext getJaxbContext(Class<?> clazz) {
    Assert.notNull(clazz, "'clazz' must not be null");
    JAXBContext jaxbContext = this.jaxbContexts.get(clazz);
    if (jaxbContext == null) {
        try {
            jaxbContext = JAXBContext.newInstance(clazz);
            this.jaxbContexts.putIfAbsent(clazz, jaxbContext);
        } catch (JAXBException ex) {
            throw new HttpMessageConversionException("Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
        }
    }
    return jaxbContext;
}
Also used : JAXBException(javax.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) JAXBContext(javax.xml.bind.JAXBContext)

Example 8 with HttpMessageConversionException

use of org.springframework.http.converter.HttpMessageConversionException in project spring-framework by spring-projects.

the class Jaxb2RootElementHttpMessageConverter method readFromSource.

@Override
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
    try {
        source = processSource(source);
        Unmarshaller unmarshaller = createUnmarshaller(clazz);
        if (clazz.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(source);
        } else {
            JAXBElement<?> jaxbElement = unmarshaller.unmarshal(source, clazz);
            return jaxbElement.getValue();
        }
    } catch (NullPointerException ex) {
        if (!isSupportDtd()) {
            throw new HttpMessageNotReadableException("NPE while unmarshalling. " + "This can happen on JDK 1.6 due to the presence of DTD " + "declarations, which are disabled.", ex);
        }
        throw ex;
    } catch (UnmarshalException ex) {
        throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

HttpMessageConversionException (org.springframework.http.converter.HttpMessageConversionException)8 JAXBException (javax.xml.bind.JAXBException)6 JAXBContext (javax.xml.bind.JAXBContext)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Marshaller (javax.xml.bind.Marshaller)2 UnmarshalException (javax.xml.bind.UnmarshalException)2 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)2 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 ArrayList (java.util.ArrayList)1 MarshalException (javax.xml.bind.MarshalException)1 XmlType (javax.xml.bind.annotation.XmlType)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Test (org.junit.Test)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpInputMessage (org.springframework.http.HttpInputMessage)1 HttpOutputMessage (org.springframework.http.HttpOutputMessage)1