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

The following examples show how to use org.springframework.beans.TypeMismatchException#getRequiredType() . 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: TypeMismatchWebErrorHandler.java    From errors-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
static List<Argument> getArguments(TypeMismatchException mismatchException) {
    List<Argument> arguments = new ArrayList<>();
    arguments.add(arg("property", getPropertyName(mismatchException)));
    arguments.add(arg("invalid", mismatchException.getValue()));
    Class<?> requiredType = mismatchException.getRequiredType();
    if (requiredType != null) {
        arguments.add(arg("expected", Classes.getClassName(requiredType)));
    }

    return arguments;
}
 
Example 2
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 3
Source File: OneOffSpringCommonFrameworkExceptionHandlerListener.java    From backstopper with Apache License 2.0 5 votes vote down vote up
protected boolean isRequiredTypeAssignableToOneOf(TypeMismatchException tme, Class<?>... allowedClasses) {
    Class<?> desiredClass = tme.getRequiredType();
    for (Class<?> allowedClass : allowedClasses) {
        if (allowedClass.isAssignableFrom(desiredClass)) {
            return true;
        }
    }

    return false;
}
 
Example 4
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 5
Source File: OneOffSpringCommonFrameworkExceptionHandlerListener.java    From backstopper with Apache License 2.0 4 votes vote down vote up
protected String extractRequiredTypeNoInfoLeak(TypeMismatchException tme) {
    if (tme.getRequiredType() == null) {
        return null;
    }

    if (isRequiredTypeAssignableToOneOf(tme, Byte.class, byte.class)) {
        return "byte";
    }

    if (isRequiredTypeAssignableToOneOf(tme, Short.class, short.class)) {
        return "short";
    }

    if (isRequiredTypeAssignableToOneOf(tme, Integer.class, int.class)) {
        return "int";
    }

    if (isRequiredTypeAssignableToOneOf(tme, Long.class, long.class)) {
        return "long";
    }

    if (isRequiredTypeAssignableToOneOf(tme, Float.class, float.class)) {
        return "float";
    }

    if (isRequiredTypeAssignableToOneOf(tme, Double.class, double.class)) {
        return "double";
    }

    if (isRequiredTypeAssignableToOneOf(tme, Boolean.class, boolean.class)) {
        return "boolean";
    }

    if (isRequiredTypeAssignableToOneOf(tme, Character.class, char.class)) {
        return "char";
    }

    if (isRequiredTypeAssignableToOneOf(tme, CharSequence.class)) {
        return "string";
    }

    return "[complex type]";
}