Reputation: 950
I am developing REST services using camel REST-DSL [in camel documentation] component.I am successful in sending JSON request for happy path and get the response on the same using (Rest-DSL and camel servlet) combination. Now as we move forward the client may sent Rest Service request with not enough values or invalid valid request ,now I am looking for ways to send 400 status code as a response from REST DSL when request is not valid.
Please advise on the way to achieve this.
Upvotes: 4
Views: 8087
Reputation: 113
As suggested by Pierre-Alban you may validate the payload in processor or service class and set the correct response code and message in processor.
Another way is to throw an exception from validator method and let the route builder handle that exception. Something like this:
onException(ValidationException.class)
.handled(true)
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
.setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
.setBody(exceptionMessage());
Upvotes: 7
Reputation: 108
I am assuming you are doing your validation in a custom processor or something similiar. If you want to send your an HTTP Error code just add the header Exchange.HTTP_RESPONSE_CODE.
For example : exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
The behaviour may depend of the engine you use. As documentation says it should work with servlet engine :
Upvotes: 8