This post has the working example of creating a REST service using Apache camel.
1) Add below maven dependencies in your pom.xml
pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <camelspring.version>2.16.0</camelspring.version> <spring.version>3.2.10.RELEASE</spring.version> </properties> <dependencies> <!-- Camel Dependencies --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>${camelspring.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cxf</artifactId> <version>${camelspring.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-servlet</artifactId> <version>${camelspring.version}</version> </dependency> <!-- End of Camel Dependencies --> <!-- Spring Dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!-- End of Spring dependencies --> <!-- Jackson dependencies --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependencies/>
2) Define CXFServlet and camel-context xml in your web.xml
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Apache Camel CXF Rest Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:camel-cxfrs-config.xml</param-value> </context-param> <servlet> <servlet-name>CXF Servlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXF Servlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
3) Define your camel-context xml
camel-cxfrs-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="userServiceBean" class="com.poc.rest.UserServiceImpl" /> <cxf:rsServer id="userSvcServer" address="/" loggingFeatureEnabled="true"> <cxf:serviceBeans> <ref bean="userServiceBean" /> </cxf:serviceBeans> <cxf:providers> <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" /> </cxf:providers> </cxf:rsServer> <camelContext id="context" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="cxfrs:bean:userSvcServer" /> <log message="Processing CXF route....http method ${header.CamelHttpMethod}" /> <log message="Processing CXF route....path is ${header.CamelHttpPath}" /> <log message="Processing CXF route....body is ${body}" /> <choice> <when> <simple>${header.operationName} == 'details'</simple> <to uri="direct:invokeDetails" /> </when> <when> <simple>${header.operationName} == 'add'</simple> <to uri="direct:invokeAdd" /> </when> </choice> </route> <route id="invokeDetails"> <from uri="direct:invokeDetails" /> <log message="Processing invokeDetails....Start ${body}" /> <bean ref="userServiceBean" method="details" /> <log message="Processing invokeDetails....End is ${body}" /> </route> <route id="invokeAdd"> <from uri="direct:invokeAdd" /> <log message="Processing invokeAdd....Start is ${body}" /> <bean ref="userServiceBean" method="add" /> <log message="Processing invokeAdd....End is ${body}" /> </route> </camelContext> </beans>
4) Define your REST interface
Rest Interface using Jax-RS
package com.poc.rest; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.poc.rest.beans.AddReq; @Path("/user") public interface UserService { @GET @Path("/details/{id}") @Produces(MediaType.APPLICATION_JSON) public Response details(@PathParam("id") String id); @POST @Path("/add") @Produces(MediaType.APPLICATION_JSON) public Response add(AddReq input); }
5) Implementation code
UserServiceImpl.java
package com.poc.rest; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import com.poc.rest.beans.AddReq; import com.poc.rest.beans.AddResp; import com.poc.rest.beans.DetailsResp; public class UserServiceImpl implements UserService { public Response details(String id) { System.out.println("UserServiceImpl Java details start"); DetailsResp resp = new DetailsResp(); resp.setId(id); resp.setName("kswaughs"); resp.setStatus("Success"); return Response.status(Status.OK). entity(resp).build(); } public Response add(AddReq req) { System.out.println("UserServiceImpl Java add start"); AddResp resp = new AddResp(); resp.setId(req.getId()); resp.setStatus("Success"); return Response.status(Status.OK). entity(resp).build(); } }
6) Testing the service. Run the maven build and generated the war with name 'camel-rest' and deployed in Jboss 7 server.
Test 1 : operation : details method : GET URL : http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/details/123 Response : { "status": "Success", "id": "12345", "name": "kswaughs" }
Test 2 : operation : add method : POST URL : http://127.0.0.1:8080/camel-rest-0.0.1-SNAPSHOT/user/add Request { "id" : "12345" , "name":"kswaughs" } Response { "id": "12345", "status": "Success" }
No comments:
Post a Comment