Example usage for org.springframework.http.converter HttpMessageNotReadableException HttpMessageNotReadableException

List of usage examples for org.springframework.http.converter HttpMessageNotReadableException HttpMessageNotReadableException

Introduction

In this page you can find the example usage for org.springframework.http.converter HttpMessageNotReadableException HttpMessageNotReadableException.

Prototype

public HttpMessageNotReadableException(String msg, HttpInputMessage httpInputMessage) 

Source Link

Document

Create a new HttpMessageNotReadableException.

Usage

From source file:org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.java

private Object read(IncomingRequest request, HttpMessageConverter<Object> converter,
        RootResourceInformation information) {

    try {//from  w ww . j  a v a 2  s  .  co  m
        return converter.read(information.getDomainType(), request.getServerHttpRequest());
    } catch (IOException o_O) {
        throw new HttpMessageNotReadableException(String.format(ERROR_MESSAGE, information.getDomainType()),
                o_O);
    }
}

From source file:org.alfresco.repo.publishing.JaxbHttpMessageConverter.java

@Override
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
    try {/*from  ww  w. j av  a  2  s.  com*/
        Unmarshaller unmarshaller = createUnmarshaller(clazz);
        if (clazz.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(source);
        } else {
            JAXBElement<?> jaxbElement = unmarshaller.unmarshal(source, clazz);
            return jaxbElement.getValue();
        }
    } 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);
    }
}

From source file:org.molgenis.util.GsonHttpMessageConverter.java

@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {
    Reader json = new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders()));
    try {//from w w w.ja va  2s  .c om
        Type typeOfT = getType();
        if (LOG.isTraceEnabled()) {
            String jsonStr = IOUtils.toString(json);
            LOG.trace("Json request:\n" + jsonStr);

            if (typeOfT != null) {
                return this.gson.fromJson(jsonStr, typeOfT);
            }

            return this.gson.fromJson(jsonStr, clazz);
        } else {
            if (typeOfT != null) {
                return this.gson.fromJson(json, typeOfT);
            }

            return this.gson.fromJson(json, clazz);
        }
    } catch (JsonSyntaxException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    } catch (JsonIOException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    } catch (JsonParseException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(json);
    }
}

From source file:org.springframework.flex.http.AmfHttpMessageConverter.java

private Object readObject(HttpInputMessage inputMessage, AmfTrace trace) throws IOException {
    Amf3Input deserializer = new Amf3Input(new SerializationContext());
    deserializer.setInputStream(inputMessage.getBody());
    deserializer.setDebugTrace(trace);//from  www  .  j  a  va  2  s  .  co  m
    try {
        return deserializer.readObject();
    } catch (ClassNotFoundException cnfe) {
        throw new HttpMessageNotReadableException(AMF_ERROR, cnfe);
    } catch (MessageException se) {
        throw new HttpMessageNotReadableException(AMF_ERROR, se);
    }
}

From source file:org.springframework.flex.http.AmfHttpMessageConverter.java

private ActionMessage readActionMessage(HttpInputMessage inputMessage, AmfTrace trace) throws IOException {
    AmfMessageDeserializer deserializer = new AmfMessageDeserializer();
    deserializer.initialize(new SerializationContext(), inputMessage.getBody(), trace);

    try {/*  www  .  java 2  s. com*/
        ActionContext context = new ActionContext();
        ActionMessage message = new ActionMessage();
        context.setRequestMessage(message);
        deserializer.readMessage(message, context);
        return message;
    } catch (ClassNotFoundException cnfe) {
        throw new HttpMessageNotReadableException(ACTION_MSG_ERROR, cnfe);
    } catch (MessageException me) {
        throw new HttpMessageNotReadableException(ACTION_MSG_ERROR, me);
    }
}

From source file:org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.java

/**
 * Create the method argument value of the expected parameter type by reading
 * from the given HttpInputMessage.//from   ww w .  java  2 s . c om
 * @param <T> the expected type of the argument value to be created
 * @param inputMessage the HTTP input message representing the current request
 * @param parameter the method parameter descriptor
 * @param targetType the target type, not necessarily the same as the method
 * parameter type, e.g. for {@code HttpEntity<String>}.
 * @return the created method argument value
 * @throws IOException if the reading from the request fails
 * @throws HttpMediaTypeNotSupportedException if no suitable message converter is found
 */
@SuppressWarnings("unchecked")
@Nullable
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,
        Type targetType)
        throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

    MediaType contentType;
    boolean noContentType = false;
    try {
        contentType = inputMessage.getHeaders().getContentType();
    } catch (InvalidMediaTypeException ex) {
        throw new HttpMediaTypeNotSupportedException(ex.getMessage());
    }
    if (contentType == null) {
        noContentType = true;
        contentType = MediaType.APPLICATION_OCTET_STREAM;
    }

    Class<?> contextClass = parameter.getContainingClass();
    Class<T> targetClass = (targetType instanceof Class ? (Class<T>) targetType : null);
    if (targetClass == null) {
        ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
        targetClass = (Class<T>) resolvableType.resolve();
    }

    HttpMethod httpMethod = (inputMessage instanceof HttpRequest ? ((HttpRequest) inputMessage).getMethod()
            : null);
    Object body = NO_VALUE;

    EmptyBodyCheckingHttpInputMessage message;
    try {
        message = new EmptyBodyCheckingHttpInputMessage(inputMessage);

        for (HttpMessageConverter<?> converter : this.messageConverters) {
            Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter
                    .getClass();
            GenericHttpMessageConverter<?> genericConverter = (converter instanceof GenericHttpMessageConverter
                    ? (GenericHttpMessageConverter<?>) converter
                    : null);
            if (genericConverter != null ? genericConverter.canRead(targetType, contextClass, contentType)
                    : (targetClass != null && converter.canRead(targetClass, contentType))) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
                }
                if (message.hasBody()) {
                    HttpInputMessage msgToUse = getAdvice().beforeBodyRead(message, parameter, targetType,
                            converterType);
                    body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse)
                            : ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse));
                    body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);
                } else {
                    body = getAdvice().handleEmptyBody(null, message, parameter, targetType, converterType);
                }
                break;
            }
        }
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("I/O error while reading input message", ex);
    }

    if (body == NO_VALUE) {
        if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod)
                || (noContentType && !message.hasBody())) {
            return null;
        }
        throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
    }

    return body;
}