Spring Data Rest - Bean validation is not applied

2019-07-24 16:04发布

I have a domain class defined as follows

@Data
@Entity
public class City {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long cityID;

    @NotBlank(message = "City name is a required field")
    private String cityName;


}

When I post to the endpoint http://localhost:8080/cities without a cityName I get a ConstraintViolationException but when I send a PUT request to the endpoint http://localhost:8080/cities/1 without a cityName I get the following exception instead of ConstraintViolationException.

{
  "timestamp": 1494510208982,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.springframework.transaction.TransactionSystemException",
  "message": "Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction",
  "path": "/cities/1"
}

So how do I get a ConstraintViolationException exception for a PUT request?

Note: I am using Spring Data Rest so the endpoints are generated by Spring. There is no custom rest controller.

2条回答
我命由我不由天
2楼-- · 2019-07-24 16:28

I think Cepr0's test work for both PUT and POST, because when you send a PUT request for a non-existing entity then Spring Data Rest uses the create method in the background. Let's assume there is no user with id=100: calling 'PUT users/100' is the same as calling 'POST users/'

When you send PUT for an existing entity, it will generate that nasty TransactionSystemException.

I'm also fighting with the Data Rest exception-handling right now, and there are a lot of inconsistency in there.

Here is my current RestErrorAttributes class, it solves most of my problems, but there is a good chance I will fond others during the following days. :)

@Component
@Slf4j
public class RestErrorAttributes extends DefaultErrorAttributes implements MessageSourceAware {

private MessageSource messageSource;

@Override
public void setMessageSource(MessageSource messageSource) {
    this.messageSource = messageSource;
}

/** {@inheritDoc} */
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {

    final Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
    // Translate default message by Locale
    String message = errorAttributes.get("message").toString();
    errorAttributes.put("message",
            messageSource.getMessage(message, null, message, LocaleContextHolder.getLocale()));
    // Extend default error message by field-errors
    addConstraintViolationDetails(errorAttributes, requestAttributes);
    return errorAttributes;
}

private void addConstraintViolationDetails(Map<String, Object> errorAttributes,
        RequestAttributes requestAttributes) {
    Throwable error = getError(requestAttributes);
    if (error instanceof ConstraintViolationException) {
        errorAttributes.put("errors",
                RestFieldError.getErrors(((ConstraintViolationException) error).getConstraintViolations()));
    }
    else if (error instanceof RepositoryConstraintViolationException) {
        errorAttributes.put("errors", RestFieldError
                .getErrors(((RepositoryConstraintViolationException) error).getErrors().getAllErrors()));
    }
}

/** {@inheritDoc} */
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception ex) {

    try {
        Throwable cause = ex;
        while (cause instanceof Exception) {
            // Handle AccessDeniedException - It cannot be handled by
            // ExceptionHandler
            if (cause instanceof AccessDeniedException) {
                response.sendError(HttpStatus.FORBIDDEN.value(), cause.getMessage());
                super.resolveException(request, response, handler, (Exception) cause);
                return new ModelAndView();
            }
            // Handle exceptions from javax validations
            if (cause instanceof ConstraintViolationException) {
                response.sendError(HttpStatus.UNPROCESSABLE_ENTITY.value(), "validation.error");
                super.resolveException(request, response, handler, (Exception) cause);
                return new ModelAndView();
            }
            // Handle exceptions from REST validator classes
            if (cause instanceof RepositoryConstraintViolationException) {
                response.sendError(HttpStatus.UNPROCESSABLE_ENTITY.value(), "validation.error");
                super.resolveException(request, response, handler, (Exception) cause);
                return new ModelAndView();
            }
            cause = ((Exception) cause).getCause();
        }
    } catch (final Exception handlerException) {
        log.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
    }
    return super.resolveException(request, response, handler, ex);
}

@Getter
@AllArgsConstructor
public static class RestFieldError {
    private String field;
    private String code;
    private String message;

    public static List<RestFieldError> getErrors(Set<ConstraintViolation<?>> constraintViolations) {
        return constraintViolations.stream().map(RestFieldError::of).collect(Collectors.toList());
    }

    public static List<RestFieldError> getErrors(List<ObjectError> errors) {
        return errors.stream().map(RestFieldError::of).collect(Collectors.toList());
    }

    private static RestFieldError of(ConstraintViolation<?> constraintViolation) {
        return new RestFieldError(constraintViolation.getPropertyPath().toString(),
                constraintViolation.getMessageTemplate(), constraintViolation.getMessage());

    }

    private static RestFieldError of(ObjectError error) {

        return new RestFieldError(error instanceof FieldError ? ((FieldError) error).getField() : null,
                error.getCode(), error.getDefaultMessage());

    }
}

}

查看更多
乱世女痞
3楼-- · 2019-07-24 16:38

My workaround for this was to setup an exception handler to handle the TransactionSystemException, unwrap the exception and handle like a regular ConstraintViolationException:

@ExceptionHandler(value = {TransactionSystemException.class})
public ResponseEntity handleTxException(TransactionSystemException ex) {
    Throwable t = ex.getCause();
    if (t.getCause() instanceof ConstraintViolationException) {
        return handleConstraintViolation((ConstraintViolationException) t.getCause(), null);
    } else {
        return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

@ExceptionHandler({ConstraintViolationException.class})
public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex,
          WebRequest request) {
    List<String> errors = new ArrayList<>();
    for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
        errors.add(violation.getRootBeanClass().getName() + " " + violation.getPropertyPath() + ": " + violation.getMessage());
    }

    return new ResponseEntity<>(errors, new HttpHeaders(), HttpStatus.CONFLICT);
}
查看更多
登录 后发表回答