Skip to main content

JBoss Fuse - Implementing WebService with Camel using CXF SOAP and Restful with CXFRS. Part One

Some friend of mine in Japan asking if I could do a simple demo on Camel CXF,  So here it is :).
This demo is about an insurance simple online claim application and status update. I will show you how to use Camel CXF to quickly setup an WebService, also setup Restful WebServices in few very easy steps.

In this demo we are going to setup a web service that takes in customer's insurance claim application, also provide another cancel function in case they have changed their mind.

One of the reason I love so much about Camel is that I don't need to deal with different context of the data, like XML, JSON... I mean they are ok, but I like POJO more, as a Java developer they are more meaningful and much easier to play with. So now we are going define some POJOs that we are going to exchange in this demo.

These two POJO contains the application information required (ClaimInput) and the result of the application submission (ClaimOutput).
package org.blogdemo.claimdemo;

import java.util.Date;

public class ClaimInput {
	
	String customerName = "";
	String customerId="";
	Date claimDate= null;
	String contactPhone = "";
	String email = "";
	String polno = "";
	int applyItem = 0;
	int claimType = 0;
	
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getCustomerId() {
		return customerId;
	}
	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}
	public Date getClaimDate() {
		return claimDate;
	}
	public void setClaimDate(Date claimDate) {
		this.claimDate = claimDate;
	}
	public String getContactPhone() {
		return contactPhone;
	}
	public void setContactPhone(String contactPhone) {
		this.contactPhone = contactPhone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPolno() {
		return polno;
	}
	public void setPolno(String polno) {
		this.polno = polno;
	}
	public int getApplyItem() {
		return applyItem;
	}
	public void setApplyItem(int applyItem) {
		this.applyItem = applyItem;
	}
	public int getClaimType() {
		return claimType;
	}
	public void setClaimType(int claimType) {
		this.claimType = claimType;
	}
	
	

}


package org.blogdemo.claimdemo;

public class ClaimOutput {

	String customerName = "";
	String claimNo = "";
	String status = "";
	
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getClaimNo() {
		return claimNo;
	}
	public void setClaimNo(String claimNo) {
		this.claimNo = claimNo;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	
	
	
}


And a third POJO ClaimStatus is for showing the status of application cancel submission.
package org.blogdemo.claimdemo;

public class ClaimStatus {
	String status = "";
	String customerID = "";
	String polno ="";
	String claimNo="";
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getCustomerID() {
		return customerID;
	}
	public void setCustomerID(String customerID) {
		this.customerID = customerID;
	}
	public String getPolno() {
		return polno;
	}
	public void setPolno(String polno) {
		this.polno = polno;
	}
	public String getClaimNo() {
		return claimNo;
	}
	public void setClaimNo(String claimNo) {
		this.claimNo = claimNo;
	}
	
	
}


Now we have all the hard part done, (Trust me, it's always defining the model that is the hardest.) We can now start on creating the web service in our camel route.

To define a web service there are 2 main steps.
1. Define an interface of the web service
2. Setup the endpoint in Camel context,

Once you get those two ready, you are pretty much done, so we will create a "ClaimService" interface, that specify two operations in our insurance claim application, apply and cancel. Have a look at the code, notice the apply method takes in ClaimInput as parameter and ClaimOutput as the return value. And for cancel method, we take in the claim number (String) and return ClaimStatus as the output.


package org.blogdemo.claimdemo;

public interface ClaimService {
	
	public ClaimOutput apply(ClaimInput input);
	public ClaimStatus cancel(String claimNo);

}

Then we go back to the camel context xml, and register our web service interface using camel-cxf component. So all you have to do it to specify the address of the web service and point the endpoint to the service class, which is the interface we just created. 

<cxf:cxfEndpoint id="claimEndpoint" address="http://localhost:9191/cxf/claim" serviceClass="org.blogdemo.insurancedemo.ClaimService" />

Lastly, the business processing part, here I am going to just place some constant value inside, but in normal world, this is where you place the actual processing logic. 

package org.blogdemo.claimdemo;

public class ClaimProcessor {
	public ClaimOutput process(ClaimInput input) throws Exception {
        // get the id of the input
    	//ClaimInput input = exchange.getIn().getBody(ClaimInput.class);

    	System.out.println(input);
    	
    	// set reply including the id
        ClaimOutput output = new ClaimOutput();
        output.setClaimNo("A00099484");
        output.setCustomerName(input.getCustomerName());
        output.setStatus("DONE");
        //exchange.getOut().setBody(output);
        return output;
    }
	
	 public ClaimStatus cancel(String claimNo) throws Exception {
	      
			ClaimStatus status = new ClaimStatus();
		   	status.setClaimNo(claimNo);
		   	status.setStatus("OK");
		   	
		   	return status;
	   }
}


Then we are going to put it together, here is what the camel route looked like. Notice we uses the operation name header to determine the process we want to pass on to.





  
  
  
  
    
        
        
        
            direct:${header.operationName}
        
    
    
        
        
    
    
        
        
    
 



And don't forget to update the pom.xml for all dependencies needed for this demo.
  
    
  		org.ow2.asm
		asm-all
		4.1
	
    
      org.apache.camel
      camel-core
      2.12.0.redhat-610379
    
    
      org.apache.camel
      camel-blueprint
      2.12.0.redhat-610379
    
    
	  org.apache.camel
	  camel-jaxb
	  2.12.0.redhat-610379
    
	
	  org.apache.camel
	  camel-jackson
	  2.12.0.redhat-610379
	    
	
    
      org.apache.camel
      camel-http4
      2.12.0.redhat-610379
	  
	
	
	 
	    org.apache.camel
	    camel-cxf
	    2.12.0.redhat-610379
	
		
	
	    org.apache.cxf
	    cxf-rt-frontend-jaxrs
	    2.7.0.redhat-610379
	
	
	
	    org.apache.cxf
	    cxf-rt-transports-http-jetty
	    2.7.0.redhat-610379
	
	
		


    
    
      org.slf4j
      slf4j-api
      1.7.5
    
    
      org.slf4j
      slf4j-log4j12
      1.7.5
    
    
      org.slf4j
      jcl-over-slf4j
      1.7.5
    
    
      log4j
      log4j
      1.2.17
    

    
    
      org.apache.camel
      camel-test-blueprint
      2.12.0.redhat-610379
      test
    
  

Now start the route by running camel:run in maven. The Registry URL will be http://localhost:9191/cxf/claim for now.


We can start the application, by using the SOAP UI tool, create a new project with the WSDL address as http://localhost:9191/cxf/claim?wsdl, then we can now play with the web service we created!

Here is the video:



There is another form of web service,  Restful web service, next part of the demo, I will show you how to build it and how to create CXF producer in Camel.

Thanks! 

Comments

Thanks for tutorial Christina, I am getting y: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: claimEndPoint of type: org.apache.camel.component.cxf.CxfEndpoint
exception , can you please help me on this
Christina Lin said…
Did you add the camel-cxf related dependencies in the pom.xml?
I was missing the POM dependency of camel-cxf ,thank you for help :)
kanav aggarwal said…
Hi Christina,

Please share code link for this project.
Thank you in advance.

Thanks,
Kanav
Unknown said…
Hi Christina,

Please share the code, am getting the error below when running the project.

[mel.test.blueprint.Main.main()] CamelBlueprintHelper INFO Deleting work directory target/bundles/1436534148555
[ERROR] *************************************
[ERROR] Error occurred while running main from: org.apache.camel.test.blueprint.Main
[ERROR]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.camel.maven.RunMojo$1.run(RunMojo.java:488)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: Gave up waiting for service (objectClass=org.apache.camel.CamelContext)
at org.apache.camel.test.blueprint.CamelBlueprintHelper.getOsgiService(CamelBlueprintHelper.java:240)
at org.apache.camel.test.blueprint.CamelBlueprintHelper.getOsgiService(CamelBlueprintHelper.java:202)
at org.apache.camel.test.blueprint.Main.doStart(Main.java:104)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.main.MainSupport.run(MainSupport.java:148)
at org.apache.camel.main.MainSupport.run(MainSupport.java:352)
at org.apache.camel.test.blueprint.Main.main(Main.java:81)
... 6 more
[ERROR] *************************************
kanav aggarwal said…
Hi friend,

Generally this issue comes when u have a jar file missing.
I mean dependency missing from your pom file or from your server.

Thanks
Kanav
Samuel Lima said…
Christina,

Do you have any example of JBoss Fuse cosuming SOAP WebService with Camel using CXF?
I've seen in the homeloan demo that consuming REST is easer than SOAP.

Thanks,
Samuel Lima
Samuel Lima said…
Sorry,

To be more clear, what I meant is a route that calls a SOAP Web Service, passes parameters, get the results and do some work with the results which are in SOAP format.

Thanks.
Christina Lin said…
Samuel, OK, sure, will put that on my list! :)

Pravin Deshmukh said…
This comment has been removed by the author.
hi Christina

I'm getting the following error:

ERROR Unable to start blueprint bundle container for claimdemo.
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'cxf: cxfendpoint'.

Can you help me?

Thanks in advance :)

Greetings!
Hello Christina,

I am getting below starrup error after successful deployment

09:45:13,578 | WARN | qtp1808615023-63 | ServletController | 123 - org.apache.cxf.cxf-rt-transports-http - 3.0.4.redhat-620133 | Can't find the the request for http://localhost:8181/cxf//'s Observer
09:47:28,089 | INFO | rint Extender: 1 | BlueprintCamelContext | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | Apache Camel 2.15.1.redhat-620133 (CamelContext: blueprintContext) is shutting down
09:47:28,119 | INFO | rint Extender: 1 | BlueprintCamelContext | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | Apache Camel 2.15.1.redhat-620133 (CamelContext: blueprintContext) uptime not started
09:47:28,119 | INFO | rint Extender: 1 | BlueprintCamelContext | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | Apache Camel 2.15.1.redhat-620133 (CamelContext: blueprintContext) is shutdown in 0.080 seconds
| ERROR | rint Extender: 1 | BlueprintContainerImpl | 21 - org.apache.aries.blueprint.core - 1.4.2 | Unable to start blueprint container for bundle claimdemo due to unresolved dependencies [(&(dataformat=json-jackson)(objectClass=org.apache.camel.spi.DataFormatResolver))]
java.util.concurrent.TimeoutExceptio

In JBoss Developer able to start successfully

Thanks in Advance!
Krishna
Unknown said…
This comment has been removed by the author.
Chanti Chanti said…
can you write post or example on csfrs REST webservice consuming remote rest url.

Regards,
Chanti

Popular posts from this blog

Red Hat JBoss Fuse/A-MQ - Fuse and A-MQ Version 6.3 GA is released!

Fuse and A-MQ 6.3 GA has just went out. Maybe, you would think this is just only a minor version release why should I care? Hold your thoughts on that! Because they have done a lot of improvements and also added many new features into this release.

Besides various bug fixes and making sure Fuse Fabric is much more stable. There are two major change in this version update:

New Tooling in JBoss Developer Studio (JBDS) 9.1 GA. Newer Apache Camel version – Camel v2.17. I was really impressed by the work put in to make developing Camel application much simpler. First is the installation of tooling itself. Now it has a all-in-one installer so you don't need to worry about which plugins you need to check. See the videos below to see the new "Getting Started" of Fuse 6.3.



And If you notice from the above video, the presentation of camel route in JBDS has also updated. It fixed some of the miss representation of logic and making it easier to read.

Old Camel Route
New Camel Route
On …

Fuse Integration Service - Setup JBDS and create first quickstart application

Before we go and start creating our first application, I want to show you how to setup your JBoss Developer Studio, create a small application from the quickstart example and then running it on Fuse Integration Service.

I am using JBoss Developer Studio version 9, you can find it here.
After download the

jboss-devstudio-9.0.0.GA-installer-eap.jar
double-click it, and start installing with default values.

After successful installation, we will need install the plugins for Fuse, on JBoss Central view, select software update, select enable early access.


And select JBoss Fuse Development for the plugin,


Click on install, and we are all set to go!

First thing first, we want to create a Fuse project to deploy on the base of Fuse Integration Service, which is OpenShift. If you have not installed it, please go back to my previous post for instructions. So on your JBDS, right click and start creating the project. Select new, maven project, if you have installed the plugin correctly, you should …

Red Hat JBoss Fuse - Getting Started with Fuse Integration Service 2.0 Tech preview

I just realized that I did not do a getting started for Fuse Integration Service 2.0 Tech preview before I did the pipeline demo, thanks for those of you who reminded me! :)

To get started with FIS 2.0, for people who has just getting to know the technology, here is how I interpret it. Basically, it's divide into two aspect,

1. Integration development, FIS uses Apache Camel as the core technology that creates, orchestrate, compose microservices into a super lightweight thin integration layer, and become the API provider and service orchestrator through exposing RESTful or messaging service endpoints. And you can choose to either package and run it with Spring-Boot or Karaf.


2. Application Deployment and Management, FIS takes advantages of OpenShift platform, and allows you to separately deploy the micro-integration service among distributed environment, at the same time takes care of the failover, high availability, load balancing and service lookup problem for you.


So, now we know …