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

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

Introduction

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

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:fi.hsl.parkandride.front.ExceptionHandlers.java

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<Map<String, Object>> jsonException(HttpServletRequest request,
        HttpMessageNotReadableException ex) {
    if (ex.getCause() instanceof JsonMappingException) {
        JsonMappingException jsonEx = (JsonMappingException) ex.getCause();
        String path = getPath(jsonEx);
        Violation violation = new Violation("TypeMismatch", path, jsonEx.getMessage());
        return handleError(request, BAD_REQUEST, ex, "Invalid input", ImmutableList.of(violation));
    }/*from w w  w. j  av  a  2 s  .  com*/
    return handleError(request, BAD_REQUEST, ex);
}

From source file:org.apereo.openlrs.controllers.xapi.XAPIExceptionHandlerAdvice.java

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody/*from  w  w w .  j  a  va2 s  .  c om*/
public XAPIErrorInfo handleHttpMessageNotReadableException(final HttpServletRequest request,
        HttpMessageNotReadableException e) {
    if (e.getCause() instanceof UnrecognizedPropertyException) {
        return this.handleUnrecognizedPropertyException(request, (UnrecognizedPropertyException) e.getCause());
    } else {
        XAPIErrorInfo result;
        if (e.getCause() instanceof JsonProcessingException) {
            final JsonProcessingException jpe = (JsonProcessingException) e.getCause();
            result = new XAPIErrorInfo(HttpStatus.BAD_REQUEST, request, jpe.getOriginalMessage());
        } else {
            result = new XAPIErrorInfo(HttpStatus.BAD_REQUEST, request, e);
        }
        this.logException(e);
        this.logError(result);
        return result;
    }
}

From source file:com.orange.cepheus.cep.controller.AdminController.java

@ExceptionHandler({ HttpMessageNotReadableException.class })
public ResponseEntity<Object> messageNotReadableExceptionHandler(HttpServletRequest req,
        HttpMessageNotReadableException exception) {
    logger.error("Request not readable: {}", exception.toString());
    StatusCode statusCode = new StatusCode();
    statusCode.setCode("400");
    statusCode.setReasonPhrase(exception.getMessage());
    if (exception.getCause() != null) {
        statusCode.setDetail(exception.getCause().getMessage());
    }/*from  w  w w  . j a  v a2s  . c o m*/
    return new ResponseEntity<>(statusCode, HttpStatus.BAD_REQUEST);
}

From source file:org.opentestsystem.shared.web.AbstractRestController.java

/**
 * Catch validation exception and return customized error message
 *///w  w  w.j  a  va2 s  .  c o  m
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public ResponseError handleInvalidFormatException(final HttpMessageNotReadableException except)
        throws LocalizedException {
    ResponseError err = null;
    if (except.getCause() instanceof InvalidFormatException) {
        final InvalidFormatException invalidFormatEx = (InvalidFormatException) except.getCause();
        final String[] fieldNames = Iterables
                .toArray(Iterables.transform(invalidFormatEx.getPath(), FIELD_NAME_SELECTOR), String.class);

        final String path = StringUtils.join(fieldNames, ".");
        String msgArg = getLocalizedMessage(path, null);
        if (path.equals(msgArg)) {
            LOGGER.warn("unable to find " + path);
            msgArg = camelToPretty(fieldNames[fieldNames.length - 1]);
        }
        err = new ResponseError(getLocalizedMessage(INVALID_FORMAT_MESSAGE,
                new String[] { msgArg, invalidFormatEx.getValue().toString() }));
    } else {
        err = new ResponseError(getLocalizedMessage(BIND_EXCEPTION_MESSAGE, null));
    }
    return err;
}