Apache Camel : Control over route startup

2019-07-27 13:40发布

问题:

i have a restful webservice configured using cxf and camel. My config xml is :

<jaxrs:server id="restContainer" address="/" staticSubresourceResolution="true"> <jaxrs:serviceBeans>  
<ref bean="FooBar" />
  </jaxrs:serviceBeans> 
 <jaxrs:providers>  
<bean class="org.apache.cxf.jaxrs.provider.JSONProvider">
  <property name="dropRootElement" value="true" /> 
 <property name="supportUnwrapped" value="true" /> 
 </jaxrs:providers>  
<camelcxf:rsServer id="rsServer" address="/" serviceClass="com.camel.example.FooBar" /> <camel:camelContext id="camelContext-1">  
<camel:route> 
 <camel:from uri="cxfrs:bean:rsServer" />  
<camel:to uri="http://localhost:8080/Google/rest/search" />  
</camel:route>  
</camel:camelContext> 

Now i have FooBar class which is exposed as a service and is like this :

@Service("Demo") @Path("/foo/bar") public class FooBar{

       @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public PoiDetailsResponse poiGetDetails(
            PoiDetailsRequest json)
    {
        System.out.println(json.getUname());
        System.out.println(json.getDeviceid());
        //do some validation and some business logic
        //return accordingly;
    }

My concern is that when i hit my server .. camelContext takes over immediately and the method present in my class is not at all touched .. infact whatever response comes from my "to" part of the route is send back to the client.. now one way is that i add multiple processor for every businesss logic. but i really want to have my method executed first and then route starts .. how can i do this ?? Also i can hit my server with whatever parameters i want, even if they are wrong (meaning wrong datatypes of variables of PoiDetailsRequest) and get any response parameters (which are not part of PoiDetailsResponse), this is ofcourse not a good thing.. please suggest something..

回答1:

You dont need to use the Camel cxfrs component if you want to expose a RS service and use the service bean. You can just use plain CXF RS for this.

The Camel cxfrs component is for when you want to let the RS service route directly into the Camel route.

If you want your method executed first, then you can from your method call Camel by using the ProducerTemplate to send a message to a Camel route using the direct endpoint.