0

I want to print only constraint message but apachecamel printing complete message like

Bean Code part

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

Router Code

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

Processor code

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());

Following is response received

<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>
ImranRazaKhan
  • 1,955
  • 5
  • 33
  • 74
  • Isn't there some kind of BeanValidationException being thrown that has APIs to get constraint message and other details. In the processor code you just work with the generic Exception class, try to find out its really type and see what API it has – Claus Ibsen Oct 06 '17 at 07:39

1 Answers1

0

Below works for me

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() );
ImranRazaKhan
  • 1,955
  • 5
  • 33
  • 74