javax.validation.ConstraintViolationException.getConstraintViolations()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(99)

本文整理了Java中javax.validation.ConstraintViolationException.getConstraintViolations()方法的一些代码示例,展示了ConstraintViolationException.getConstraintViolations()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ConstraintViolationException.getConstraintViolations()方法的具体详情如下:
包路径:javax.validation.ConstraintViolationException
类名称:ConstraintViolationException
方法名:getConstraintViolations

ConstraintViolationException.getConstraintViolations介绍

[英]Returns the set of constraint violations reported during a validation.
[中]返回验证期间报告的约束冲突集。

代码示例

代码示例来源:origin: apache/incubator-dubbo

protected Response handleConstraintViolationException(ConstraintViolationException cve) {
    ViolationReport report = new ViolationReport();
    for (ConstraintViolation cv : cve.getConstraintViolations()) {
      report.addConstraintViolation(new RestConstraintViolation(
          cv.getPropertyPath().toString(),
          cv.getMessage(),
          cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString()));
    }
    // TODO for now just do xml output
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(report).type(ContentType.TEXT_XML_UTF_8).build();
  }
}

代码示例来源:origin: apache/incubator-dubbo

protected Response handleConstraintViolationException(ConstraintViolationException cve) {
    ViolationReport report = new ViolationReport();
    for (ConstraintViolation cv : cve.getConstraintViolations()) {
      report.addConstraintViolation(new RestConstraintViolation(
          cv.getPropertyPath().toString(),
          cv.getMessage(),
          cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString()));
    }
    // TODO for now just do xml output
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(report).type(ContentType.TEXT_XML_UTF_8).build();
  }
}

代码示例来源:origin: hs-web/hsweb-framework

@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
ResponseMessage handleConstraintViolationException(ConstraintViolationException e) {
  SimpleValidateResults results = new SimpleValidateResults();
  for (ConstraintViolation<?> violation : e.getConstraintViolations()) {
    results.addResult(violation.getPropertyPath().toString(), violation.getMessage());
  }
  List<ValidateResults.Result> errorResults = results.getResults();
  return ResponseMessage
      .error(400, errorResults.isEmpty() ? "" : errorResults.get(0).getMessage())
      .result(errorResults);
}

代码示例来源:origin: linlinjava/litemall

@ExceptionHandler(ValidationException.class)
@ResponseBody
public Object badArgumentHandler(ValidationException e) {
  e.printStackTrace();
  if (e instanceof ConstraintViolationException) {
    ConstraintViolationException exs = (ConstraintViolationException) e;
    Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
    for (ConstraintViolation<?> item : violations) {
      String message = ((PathImpl) item.getPropertyPath()).getLeafNode().getName() + item.getMessage();
      return ResponseUtil.fail(402, message);
    }
  }
  return ResponseUtil.badArgumentValue();
}

代码示例来源:origin: Netflix/conductor

private static ErrorResponse constraintViolationExceptionToErrorResponse(ConstraintViolationException exception) {
  ErrorResponse errorResponse = new ErrorResponse();
  errorResponse.setStatus(Response.Status.BAD_REQUEST.getStatusCode());
  errorResponse.setMessage("Validation failed, check below errors for detail.");
  List<ValidationError> validationErrors = new ArrayList<>();
  exception.getConstraintViolations().forEach(e ->
  validationErrors.add(new ValidationError(getViolationPath(e), e.getMessage(), getViolationInvalidValue(e.getInvalidValue()))));
  errorResponse.setValidationErrors(validationErrors);
  return errorResponse;
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testStartWorkflowNull() {
  try{
    workflowService.startWorkflow(null);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("StartWorkflowRequest cannot be null"));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testGetExecutionStatusNoWorkflowId() {
  try{
    workflowService.getExecutionStatus("", true);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("WorkflowId cannot be null or empty."));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testInvalidPauseWorkflow() {
  try{
    workflowService.pauseWorkflow(null);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("WorkflowId cannot be null or empty."));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testTerminateWorkflowNull() {
  try{
    workflowService.terminateWorkflow(null, null);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("WorkflowId cannot be null or empty."));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testPoll(){
  try{
    taskService.poll(null, null, null);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains( "TaskType cannot be null or empty."));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testRestartWorkflowNull(){
  try{
    workflowBulkService.restart(null, false);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("WorkflowIds list cannot be null."));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testRetryWorkflowNull(){
  try{
    workflowBulkService.retry(null);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("WorkflowIds list cannot be null."));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testGetWorkflowsNoName() {
  try{
    workflowService.getWorkflows("", "c123", true, true);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("Workflow name cannot be null or empty"));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testInvalidDeleteWorkflow() {
  try{
    workflowService.deleteWorkflow(null, true);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("WorkflowId cannot be null or empty."));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testInvalidWorkflowNameGetRunningWorkflows() {
  try{
    workflowService.getRunningWorkflows(null, 123, null, null);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("Workflow name cannot be null or empty."));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testGetTask(){
  try{
    taskService.getTask(null);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("TaskId cannot be null or empty."));
    throw ex;
  }
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testRemoveEventHandlerStatus(){
  try{
    eventService.removeEventHandlerStatus(null);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("EventHandler name cannot be null or empty."));
    throw ex;
  }
  fail("eventService.removeEventHandlerStatus did not throw ConstraintViolationException !");
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testUpdateEventHandler(){
  try{
    eventService.updateEventHandler(null);
  } catch (ConstraintViolationException ex){
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("EventHandler cannot be null."));
    throw ex;
  }
  fail("eventService.updateEventHandler did not throw ConstraintViolationException !");
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testUpdateTaskDefNull() {
  try{
    metadataService.updateTaskDef(null);
  } catch (ConstraintViolationException ex) {
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("TaskDef cannot be null"));
    throw ex;
  }
  fail("metadataService.updateTaskDef did not throw ConstraintViolationException !");
}

代码示例来源:origin: Netflix/conductor

@Test(expected = ConstraintViolationException.class)
public void testValidateEventNull() {
  try{
    metadataService.addEventHandler(null);
  } catch (ConstraintViolationException ex) {
    assertEquals(1, ex.getConstraintViolations().size());
    Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
    assertTrue(messages.contains("EventHandler cannot be null"));
    throw ex;
  }
  fail("metadataService.addEventHandler did not throw ConstraintViolationException !");
}

相关文章