如何仅从BeanValidationException打印约束消息

时间:2017-10-06 01:08:52

标签: apache-camel bean-validation

我想只打印约束消息但是打印完成消息的apachecamel

Bean Code部分

@NotNull(message="Validation Error name value Missing.")
private String name;

路由器代码

    onException(BeanValidationException.class) 
    .handled(true) 
    .process( new FailedResponseProcessor() );

处理器代码

public void process(Exchange exchange) throws Exception {
    Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); 
    Response response = new Response();
    response.setRequestStatus("Failed");
    response.setRequestMessage(e.getMessage());

以下是收到的回复

<response>
  <requestStatus>Failed</requestStatus>
  <requestMessage>Validation failed for: org.my.Request@1b8a0be3 errors: [property: name; value: null; constraint: Validation Error name value Missing.; ]. Exchange[ID-WCB00073679-49595-1507251546181-0-1]</requestMessage>
</response>

1 个答案:

答案 0 :(得分:0)

以下适用于我

BeanValidationException bve = (BeanValidationException) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
Set<ConstraintViolation<Object>> constraintViolations   =   bve.getConstraintViolations();
ConstraintViolation<Object> constraintViolation = constraintViolations.iterator().next();
System.out.println( constraintViolation.getMessage() );