Wednesday, October 29, 2014

Exposing Rest end point using Camel CXFRS

Exposing a Rest end point using Camel cxfrs is quite simple if you know the basics.

I will tell you how to do that in step by step basis:

1) Create a resouce class which act as dummy endpoint to receive your REST,  calling it dummy class as  request as it is basically handeled by Camel Route. It is still needed as that is how camel rest work.


2)  Create a Camel Route to handle the REST Request.  We are going to use cxfrs  for this.

3) Final Step is to using Spring xml based or Java based configuration. for final configuration.

Note :Assumption is you already have all the dependency in your maven pom file  or all the required jars in  your class path. Refer camel cxfrs for more detail

Lets Begin:

1) Creating the Resource class:
-----------------------------------------------------
package com.sj.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* Rest Resource.
*/
@Path("/data")
public class MyResources {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getData() throws Exception{
        // Actual response of this method will provided by the Camel route
        return null;
    }
}
----------------------------------------------------------------

2) Create the Camel Route (Which will actually do the request  handling and give back the Response)
 ------------------------------------------------------------------------------
package com.sj.route;                                                                                                                      
                                                                                                                                                         
import org.apache.camel.ExchangePattern;                                                                                    
import org.apache.camel.LoggingLevel;                                                                                         
import org.apache.camel.builder.RouteBuilder;                                                                              
import org.springframework.beans.factory.annotation.Autowired;                                                 
                                                                                                                                                          
/**                                                                                                                                                     
 * This class represent the Route definition                                                                                     
 */                                                                                                                                                      
public class CustomDataRoute extends RouteBuilder {                                                                  

    @Override                                                                                                                                     
    public void configure() throws Exception {                                                                                  

        from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer")                      
                .setExchangePattern(ExchangePattern.InOut)
                .log(LoggingLevel.INFO,  "Inside the Route  ")
                .process(new Processor() {
                   
                    @Override
                    public void process(Exchange exchange) throws Exception {
                     // this is what will be returned
                     exchange.getIn().setBody("This is the Response Data: Hello Rest");

                    }
                })
                .log(LoggingLevel.INFO,"Exiting the Route ")
                .end();

    }

}
 ---------------------------------------------------------------------------

3) Last and final Step to configure your cxfrs server using your Spring xml.
Let say your spring configuration file name is : camel-server.xml

--------------------------------
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
    ">
 
  <!-- Defined the server endpoint to create the cxf-rs consumer -->
  <cxf:rsServer id="rsServer" address="http://localhost:8080/sj"
      serviceClass="com.sj.resources.MyResources"
      loggingFeatureEnabled="true" 
loggingSizeLimit="20" 
skipFaultLogging="true"/>
 

  
 <bean id="customDataRoute" class="com.sj.route.CustomDataRoute" />
  

<!-- The camel route context -->
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
    <routeBuilder ref="customDataRoute" />
</camelContext>
   
</beans>

----------------------------------------------------------------------------------

If you want to  use Java based configuration then it will be something like this :

 @Bean(name = "rsServer")
    JAXRSServerFactoryBean restServer() {
        SpringJAXRSServerFactoryBean sf = new SpringJAXRSServerFactoryBean();
        sf.setServiceBean(Arrays.asList(new MyResources())); // you can add more resource if you want.
        sf.setAddress("http://localhost:8080/sj");
        JacksonJaxbJsonProvider jacksonJaxbJsonProvider= new JacksonJaxbJsonProvider();
        jacksonJaxbJsonProvider.setMapper(new ObjectMapper());
        sf.setProviders(Arrays.asList(jacksonJaxbJsonProvider)); // you can add more provider

        return sf;
    }
 -------------------------------------------------------------------

That is it.  Now you can  deploy your application and

call   http://localhost:8080/sj/data



No comments:

Post a Comment