Java Code Examples for org.springframework.beans.TypeMismatchException#getValue()

The following examples show how to use org.springframework.beans.TypeMismatchException#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomRestExceptionHandler.java    From xxproject with Apache License 2.0 5 votes vote down vote up
@Override
protected ResponseEntity<Object> handleTypeMismatch(final TypeMismatchException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
    logger.info(ex.getClass().getName());
    //
    final String error = ex.getValue() + " value for " + ex.getPropertyName() + " should be of type " + ex.getRequiredType();

    final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error);
    return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
}
 
Example 2
Source File: CustomRestExceptionHandler.java    From tutorials with MIT License 5 votes vote down vote up
@Override
protected ResponseEntity<Object> handleTypeMismatch(final TypeMismatchException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
    logger.info(ex.getClass().getName());
    //
    final String error = ex.getValue() + " value for " + ex.getPropertyName() + " should be of type " + ex.getRequiredType();

    final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error);
    return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
}
 
Example 3
Source File: OneOffSpringCommonFrameworkExceptionHandlerListener.java    From backstopper with Apache License 2.0 4 votes vote down vote up
protected ApiExceptionHandlerListenerResult handleTypeMismatchException(
    TypeMismatchException ex,
    List<Pair<String, String>> extraDetailsForLogging,
    boolean addBaseExceptionMessageToLoggingDetails
) {
    // The metadata will only be used if it's a 400 error.
    Map<String, Object> metadata = new LinkedHashMap<>();

    if (addBaseExceptionMessageToLoggingDetails) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
    }

    String badPropName = extractPropertyName(ex);
    if (badPropName == null) {
        badPropName = ex.getPropertyName();
    }
    String badPropValue = (ex.getValue() == null) ? null : String.valueOf(ex.getValue());
    String requiredTypeNoInfoLeak = extractRequiredTypeNoInfoLeak(ex);

    extraDetailsForLogging.add(Pair.of("bad_property_name", badPropName));
    if (badPropName != null) {
        metadata.put("bad_property_name", badPropName);
    }
    extraDetailsForLogging.add(Pair.of("bad_property_value", String.valueOf(ex.getValue())));
    if (badPropValue != null) {
        metadata.put("bad_property_value", badPropValue);
    }
    extraDetailsForLogging.add(Pair.of("required_type", String.valueOf(ex.getRequiredType())));
    if (requiredTypeNoInfoLeak != null) {
        metadata.put("required_type", requiredTypeNoInfoLeak);
    }

    // ConversionNotSupportedException is a special case of TypeMismatchException that should be treated as
    //      a 500 internal service error. See Spring's DefaultHandlerExceptionResolver and/or
    //      ResponseEntityExceptionHandler for verification.
    if (ex instanceof ConversionNotSupportedException) {
        // We can add even more context log details if it's a MethodArgumentConversionNotSupportedException.
        if (ex instanceof MethodArgumentConversionNotSupportedException) {
            MethodArgumentConversionNotSupportedException macnsEx = (MethodArgumentConversionNotSupportedException)ex;
            extraDetailsForLogging.add(Pair.of("method_arg_name", macnsEx.getName()));
            extraDetailsForLogging.add(Pair.of("method_arg_target_param", macnsEx.getParameter().toString()));
        }

        return handleError(projectApiErrors.getGenericServiceError(), extraDetailsForLogging);
    }
    else {
        // All other TypeMismatchExceptions should be treated as a 400, and we can/should include the metadata.

        // We can add even more context log details if it's a MethodArgumentTypeMismatchException.
        if (ex instanceof MethodArgumentTypeMismatchException) {
            MethodArgumentTypeMismatchException matmEx = (MethodArgumentTypeMismatchException)ex;
            extraDetailsForLogging.add(Pair.of("method_arg_name", matmEx.getName()));
            extraDetailsForLogging.add(Pair.of("method_arg_target_param", matmEx.getParameter().toString()));
        }

        return handleError(
            new ApiErrorWithMetadata(projectApiErrors.getTypeConversionApiError(), metadata),
            extraDetailsForLogging
        );
    }
}