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:com.ericsson.eiffel.remrem.publish.config.GsonHttpMessageConverterWithValidate.java

@Override
protected Object readInternal(Type resolvedType, Reader reader) throws Exception {
    try {// w w  w.  ja  va2  s  .  com
        final String json = IOUtils.toString(reader);
        // do the actual validation
        final ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
        mapper.readTree(json);
        return this.gson.fromJson(json, resolvedType);
    } catch (JsonParseException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    }

}

From source file:io.lavagna.web.helper.GsonHttpMessageConverter.java

@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException {
    try (Reader reader = new InputStreamReader(inputMessage.getBody(), StandardCharsets.UTF_8)) {
        return gson.fromJson(reader, clazz);
    } catch (JsonSyntaxException e) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + e.getMessage(), e);
    }//from  ww w. ja  v a  2 s . c o  m
}

From source file:com.ericsson.eiffel.remrem.generate.config.GsonHttpMessageConverterWithValidate.java

@Override
protected Object readInternal(Type resolvedType, Reader reader) throws Exception {
    try {/*from  w ww . j  a  v a 2 s.c  o  m*/
        final String json = IOUtils.toString(reader);
        // do the actual validation
        final ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
        mapper.readTree(json);
        return this.gson.fromJson(json, resolvedType);
    } catch (JsonParseException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    }
}

From source file:com.qcadoo.view.internal.JsonHttpMessageConverter.java

@Override
protected JSONObject readInternal(final Class<? extends JSONObject> clazz, final HttpInputMessage inputMessage)
        throws IOException {
    String body = IOUtils.toString(inputMessage.getBody(), CHARSET.name());
    try {/*from w  ww.  ja  v a  2  s. c o m*/
        return new JSONObject(body);
    } catch (JSONException e) {
        throw new HttpMessageNotReadableException(e.getMessage(), e);
    }
}

From source file:nl.flotsam.calendar.web.UriHttpMessageConverter.java

@Override
protected URI readInternal(Class<? extends URI> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {
    InputStream in = null;//from www  . j a  va2s. com
    try {
        in = inputMessage.getBody();
        String uri = IOUtils.toString(in);
        if (inputMessage.getHeaders().getContentType() == MediaType.APPLICATION_FORM_URLENCODED) {
            uri = URLDecoder.decode(uri, "UTF-8");
        }
        return new URI(uri);
    } catch (URISyntaxException urie) {
        throw new HttpMessageNotReadableException("Failed to parse incoming String into a URI.", urie);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:sys.core.jackson.MappingJacksonHttpMessageConverter.java

@Override
protected Object readInternal(Class<?> clase, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {

    JavaType javaType = getJavaType(clase);
    try {//from www . ja  v a2  s.c om
        return this.objectMapper.readValue(inputMessage.getBody(), javaType);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    }
}

From source file:edu.wisc.http.converter.xml.JaxbMarshallingHttpMessageConverter.java

@Override
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
    try {/* ww  w.j av  a  2s .  c  o m*/
        final Unmarshaller unmarshaller = this.jaxbContext.createUnmarshaller();

        Object result = unmarshaller.unmarshal(source, clazz);
        if (result instanceof JAXBElement<?>) {
            result = ((JAXBElement<?>) result).getValue();
        }

        if (!clazz.isInstance(result)) {
            throw new TypeMismatchException(result, clazz);
        }
        return result;
    } catch (JAXBException e) {
        throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", e);
    }
}

From source file:org.openspaces.rest.utils.ControllerUtils.java

public static SpaceDocument[] createSpaceDocuments(String type, String body, GigaSpace gigaSpace)
        throws TypeNotFoundException {
    HashMap<String, Object>[] propertyMapArr;
    try {/*w  w  w .  j  a  va 2s  .com*/
        //if single json object convert it to array
        String data = body;
        if (!body.startsWith("[")) {
            StringBuilder sb = new StringBuilder(body);
            sb.insert(0, "[");
            sb.append("]");
            data = sb.toString();
        }
        //convert to json
        propertyMapArr = mapper.readValue(data, typeRef);
    } catch (Exception e) {
        throw new HttpMessageNotReadableException(e.getMessage(), e.getCause());
    }
    SpaceDocument[] documents = new SpaceDocument[propertyMapArr.length];
    for (int i = 0; i < propertyMapArr.length; i++) {
        Map<String, Object> typeBasedProperties = getTypeBasedProperties(type, propertyMapArr[i], gigaSpace);
        documents[i] = new SpaceDocument(type, typeBasedProperties);
    }
    return documents;
}

From source file:org.springframework.data.rest.webmvc.json.DomainObjectReader.java

/**
 * Reads the given input stream into an {@link ObjectNode} and applies that to the given existing instance.
 * /* ww w. j  av a 2s. co m*/
 * @param request must not be {@literal null}.
 * @param target must not be {@literal null}.
 * @param mapper must not be {@literal null}.
 * @return
 */
public <T> T read(InputStream source, T target, ObjectMapper mapper) {

    Assert.notNull(target, "Target object must not be null!");
    Assert.notNull(source, "InputStream must not be null!");
    Assert.notNull(mapper, "ObjectMapper must not be null!");

    try {
        return doMerge((ObjectNode) mapper.readTree(source), target, mapper);
    } catch (Exception o_O) {
        throw new HttpMessageNotReadableException("Could not read payload!", o_O);
    }
}

From source file:org.springframework.cloud.aws.messaging.endpoint.NotificationMessageHandlerMethodArgumentResolver.java

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object doResolveArgumentFromNotificationMessage(JsonNode content, HttpInputMessage request,
        Class<?> parameterType) {
    if (!"Notification".equals(content.get("Type").asText())) {
        throw new IllegalArgumentException(
                "@NotificationMessage annotated parameters are only allowed for method that receive a notification message.");
    }/*  w ww  . ja va 2  s.c o  m*/

    MediaType mediaType = getMediaType(content);
    String messageContent = content.findPath("Message").asText();

    for (HttpMessageConverter<?> converter : this.messageConverter) {
        if (converter.canRead(parameterType, mediaType)) {
            try {
                return converter.read((Class) parameterType,
                        new ByteArrayHttpInputMessage(messageContent, mediaType, request));
            } catch (Exception e) {
                throw new HttpMessageNotReadableException(
                        "Error converting notification message with payload:" + messageContent, e);
            }
        }
    }

    throw new HttpMessageNotReadableException(
            "Error converting notification message with payload:" + messageContent);
}