I have a cxf server defined in camel.xml as:
<cxf:rsServer id="rsServer" address="http://0.0.0.0:9090/orderservice"
serviceClass="my.pakcage.myClass" />
I have a REST service defined as:
@POST
@Path("/orders/{id}/something")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response changeOrderSomething(@PathParam("id") String orderId, @Body String somethingPayload) {
return null;
}
In Java DSL I have camel route as:
from("rsServer?bindingStyle=Default").log("rsServerlogging: ${body}")
.recipientList(simple("direct-vm:${header.operationName}"));
I am calling POST on /orders/1/something
and in the body I am passing a json: {"somethingId":"3"}
Later I have a route which accepts direct-vm:changeOrderSomething
and passes it to a processor:
@Override
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
LOG.info("exchange.getIn body is: {}", body);
}
The problem I am facing is that here in the log, body is only "1" which is the path param, which means I am not getting what I am passing as body in the POST request. If I change the order of parameters in REST service as @Body String somethingPayload, @PathParam("id") String orderId
, I am getting the json that I passed but not the path param.
What do I do to get everything that I pass as parameters in REST?
UPDATE
I made a mistake calling an Object[] somewhere so it threw exception:
org.apache.cxf.interceptor.Fault: Failed to invoke method: [0] on null due to: java.lang.IndexOutOfBoundsException: Key: 0 not found in bean: 1 of type: java.lang.String using OGNL path [[0]] while invoking public javax.ws.rs.core.Response my.package.myClass.changeOrderSomething(java.lang.String,java.lang.String) with params [1, {
"somethingId":"3"}]
I think this means that the REST webservice is picking up the parameters, but only the first one is passed on to direct-vm:${header.operationName}
somehow. Maybe I am missing something here?
So I figured it out. The input POST body can be put as the first argument. This makes the Exchange object's body as the passed body. The @PathParam
or any other parameters can be placed later and accessed through exchange.getIn().getHeader("id")
since all such parameters are accessed from headers.