Java Code Examples for javax.validation.ConstraintViolationException#getMessage()

The following examples show how to use javax.validation.ConstraintViolationException#getMessage() . 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: ConstraintViolationExceptionMapper.java    From rest-utils with Apache License 2.0 6 votes vote down vote up
@Override
public Response toResponse(ConstraintViolationException exception) {
  final ErrorMessage message;
  if (exception instanceof RestConstraintViolationException) {
    RestConstraintViolationException restException = (RestConstraintViolationException)exception;
    message = new ErrorMessage(restException.getErrorCode(), restException.getMessage());
  } else {
    String violationMessage = ConstraintViolations.formatUntyped(
        exception.getConstraintViolations()
    );
    if (violationMessage == null || violationMessage.length() == 0) {
      violationMessage = exception.getMessage();
    }
    message = new ErrorMessage(
        UNPROCESSABLE_ENTITY_CODE,
        violationMessage
    );
  }

  return Response.status(UNPROCESSABLE_ENTITY_CODE)
      .entity(message)
      .build();
}
 
Example 2
Source File: ValidationResponse.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public ValidationResponse(final ConstraintViolationException cause) {
  super(false, new ArrayList<>());
  //noinspection ThrowableResultOfMethodCallIgnored
  checkNotNull(cause);
  Set<ConstraintViolation<?>> violations = cause.getConstraintViolations();
  if (violations != null && !violations.isEmpty()) {
    for (ConstraintViolation<?> violation : violations) {
      List<String> entries = new ArrayList<>();
      // iterate path to get the full path
      Iterator<Node> it = violation.getPropertyPath().iterator();
      while (it.hasNext()) {
        Node node = it.next();
        if (ElementKind.PROPERTY == node.getKind() || (ElementKind.PARAMETER == node.getKind() && !it.hasNext())) {
          if (node.getKey() != null) {
            entries.add(node.getKey().toString());
          }
          entries.add(node.getName());
        }
      }
      if (entries.isEmpty()) {
        if (messages == null) {
          messages = new ArrayList<>();
        }
        messages.add(violation.getMessage());
      }
      else {
        if (errors == null) {
          errors = new HashMap<>();
        }
        errors.put(Joiner.on('.').join(entries), violation.getMessage());
      }
    }
  }
  else if (cause.getMessage() != null) {
    messages = new ArrayList<>();
    messages.add(cause.getMessage());
  }
}
 
Example 3
Source File: SoftwareModuleAddUpdateWindow.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private void addNewBaseSoftware() {
    final String name = nameTextField.getValue();
    final String version = versionTextField.getValue();
    final String vendor = vendorTextField.getValue();
    final String description = descTextArea.getValue();
    final String type = typeComboBox.getValue() != null ? typeComboBox.getValue().toString() : null;

    final SoftwareModuleType softwareModuleTypeByName = softwareModuleTypeManagement.getByName(type)
            .orElseThrow(() -> new EntityNotFoundException(SoftwareModuleType.class, type));
    final SoftwareModuleCreate softwareModule = entityFactory.softwareModule().create()
            .type(softwareModuleTypeByName).name(name).version(version).description(description).vendor(vendor);
    final SoftwareModule newSoftwareModule;
    try {
        newSoftwareModule = softwareModuleManagement.create(softwareModule);
    } catch (ConstraintViolationException ex) {
        String message = "This SoftwareModuleName is not valid! "
                + "Please choose a SoftwareModuleName without the characters /, ?, #, blank, and quota chars"
                + ex.getMessage();
        LOGGER.warn(message, ex);
        uiNotifcation.displayValidationError(i18n.getMessage("message.save.fail", name + ":" + version));
        return;
    }
    eventBus.publish(this, new SoftwareModuleEvent(BaseEntityEventType.ADD_ENTITY, newSoftwareModule));
    uiNotifcation.displaySuccess(i18n.getMessage("message.save.success",
            newSoftwareModule.getName() + ":" + newSoftwareModule.getVersion()));
    softwareModuleTable.setValue(Sets.newHashSet(newSoftwareModule.getId()));
}
 
Example 4
Source File: GenieExceptionMapper.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * Handle constraint violation exceptions.
 *
 * @param cve The exception to handle
 * @return A {@link ResponseEntity} instance
 */
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<GeniePreconditionException> handleConstraintViolation(
    final ConstraintViolationException cve
) {
    this.countExceptionAndLog(cve);
    return new ResponseEntity<>(
        new GeniePreconditionException(cve.getMessage(), cve),
        HttpStatus.PRECONDITION_FAILED
    );
}
 
Example 5
Source File: ValidationController.java    From blog-tutorials with MIT License 4 votes vote down vote up
@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
ResponseEntity<String> handleConstraintViolationException(ConstraintViolationException e) {
  return new ResponseEntity<>("Validation Error: " + e.getMessage(), HttpStatus.BAD_REQUEST);
}
 
Example 6
Source File: ValidateParametersController.java    From code-examples with MIT License 4 votes vote down vote up
@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
String handleConstraintViolationException(ConstraintViolationException e) {
  return "not valid due to validation error: " + e.getMessage();
}