I am new to Apache Camel and have been struggling to define a Route for a REST endpoint where I intend to take a request, perform some filtering and processing and return a response. Below is my sample code which does not work, what am I doing wrong ? And are there examples / documentation for me to achieve similar things i.e. respond with failure response codes in case the request failed validation etc.?
public class RestRoute extends RouteBuilder {
public static final String DIRECT_HELLO = "direct:hello";
private final UpperCaseProcessor upperCaseProcessor;
public RestRoute(UpperCaseProcessor upperCaseProcessor) {
this.upperCaseProcessor = upperCaseProcessor;
}
@Override
public void configure() throws Exception {
rest("/myapp")
.get("/hello").param().dataType("string").name("name").endParam().to(DIRECT_HELLO);
from(DIRECT_HELLO)
.tracing()
.filter(exchange -> exchange.getIn().getBody() instanceof String)
.process(upperCaseProcessor)
.setHeaders(Exchange.HTTP_RESPONSE_CODE, constant(200));
}
}
While this may not be a comprehensive answer to all the questions I had asked in my query (since I am still learning) , there is one particular reason why the above code did not work.
In the above code I was checking for the payload to be of type string on the filter
.filter(exchange -> exchange.getIn().getBody() instanceof String)whereas Rest Path Query Params arenot includedin the body but in theheaders. The way to fix the above code is to change the filter line to :In the code example the request was getting filtered out and not getting propagated to the processor. When that happens apache-camel's behaviour is to return
204 No Contentin the response.My interpretation of the rest routes is that Camel will return the output of whatever route you set and unlike Java
Streamswhere you need to be careful to add a terminating operation (likecollect()) in order to have the stream expression actually evaluate ( since streams is lazy evaluation) , with camel routes no termination operation is needed and the o/p will just be whatever the end of your route defined by you is.There however seems to be some default behaviour around REST response codes being returned.