About
This example demonstrates how you can use CXF to expose a web service in Camel using code first approach.
Implementation
The web service we want to expose is defined as an interface which has 2 operations:
public interface IncidentService {
/**
* Operation to report an incident
*/
OutputReportIncident reportIncident(InputReportIncident input);
/**
* Operation to get the status of an incident
*/
OutputStatusIncident statusIncident(InputStatusIncident input);
}
In this example we are not using any JAX-WS annotations. You can use those annotations to fine control the web service wsdl contract.
In the Camel route we expose this web service very easily using the Camel CXF component.
All we have to do is to define an endpoint uri in the format
cxf:/incident?serviceClass=org.apache.camel.example.cxf.incident.IncidentService
This means Camel will expose the web service using the relative address /incident and the serviceClass parameter links to the interface which defines the code first approach.
In this example we want to be flexible, so if we add a 3rd operation to the web service we want it to be easily to add a route to handle this operation. Therefore we use the Recipient List EIP pattern to route to the route which handles the given operation. Notice how we use a Direct endpoint to link the routes.
public class CamelRoute extends RouteBuilder {
private String uri = "cxf:/incident?serviceClass=" + IncidentService.class.getName();
@Override
public void configure() throws Exception {
from(uri)
.to("log:input")
.recipientList(simple("direct:${header.operationName}"));
from("direct:reportIncident")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
String id = exchange.getIn().getBody(InputReportIncident.class).getIncidentId();
OutputReportIncident output = new OutputReportIncident();
output.setCode("OK;" + id);
exchange.getOut().setBody(output);
}
})
.to("log:output");
from("direct:statusIncident")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
OutputStatusIncident output = new OutputStatusIncident();
output.setStatus("IN PROGRESS");
exchange.getOut().setBody(output);
}
})
.to("log:output");
}
}
Spring XML
In the Spring XML file we have to import some CXF mandatory imports. Notice we use the cxf-servlet to leverage HTTP Servlet with CXF.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<package>org.apache.camel.example.cxf</package>
</camelContext>
</beans>
Running the example
TODO: How to deploy in Tomcat
You can then use SoapUI or another web service client and send a request to the http://localhost:9080/camel-example-cxf-proxy/webservices/incident url. The wsdl is located at: http://localhost:9080/camel-example-cxf-proxy/webservices/incident?wsdl.
The console should then output progress.
Sample output
TOOD: sample output
Here is some sample output from the console:
See Also