Search Tutorials


Apache Camel + Apache CXF SOAP Webservices Hello World Example | JavaInUse

Apache Camel CXF WebService using Spring DSL Hello World Example

In this post we will implement a simple Apache Camel CXF webservice and deploy it on JBoss Fuse.

Apache Camel - Table of Contents

File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip EIP Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example Calling and Consuming Webservices using Apache Camel Apache Camel Tutorial - Send SMTP Email Using Gmail Apache Camel Tutorial - SEDA component Hello World Example Spring Boot + Apache Camel + RabbitMQ - Hello World Example Apache Camel Tutorial - Idempotent Consumer using MemoryIdempotentRepository and FileIdempotentRepository Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + Transaction Management Example

Video

This tutorial is explained in the below Youtube Video.

Lets Begin

Create the maven project as follows-

camel15_6
The classes in package com.javainuse are generated classes from wsdl.
The pom.xml will be as follows-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javainuse</groupId>
	<artifactId>SoapExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>bundle</packaging>

	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.12.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-cxf</artifactId>
			<version>2.12.0</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.6</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
	</dependencies>


	<build>
		<plugins>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
			</plugin>


			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<extensions>true</extensions>
				<version>2.4.0</version>

			</plugin>

			
			<plugin>
				<groupId>org.apache.cxf</groupId>
				<artifactId>cxf-codegen-plugin</artifactId>

				<executions>
					<execution>
						<id>generate-sources</id>
						<phase>generate-sources</phase>
						<configuration>
							<sourceRoot>/src/main/java</sourceRoot>
							<wsdlOptions>
								<wsdlOption>
									<wsdl>/src/main/resources/wsdl/test.wsdl</wsdl>
								</wsdlOption>
							</wsdlOptions>
						</configuration>
						<goals>
							<goal>wsdl2java</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			
		</plugins>
	</build>

</project>




Next we define a "contract-first" webservice using WSDL. In the contract-first web service, the "contract" i.e a WSDL definition of operations and endpoints and XML schema of the messages is created first, without actually writing any service code.
Next we define the wsdl file that takes a String as an input and returns another string as output.
The WSDL is defined as follows-
<?xml version="1.0" encoding="ISO-8859-1"?>

<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://javainuse.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	targetNamespace="http://javainuse.com">

	<wsdl:types>
		<xs:schema targetNamespace="http://javainuse.com">
			<xs:element name="inputSOATest">
				<xs:complexType>
					<xs:sequence>
						<xs:element type="xs:string" name="test" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="outputSOATest">
				<xs:complexType>
					<xs:sequence>
						<xs:element type="xs:string" name="result" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:schema>
	</wsdl:types>


<!--Define input and output parameters -->
	<wsdl:message name="inputSOATest">
		<wsdl:part name="in" element="tns:inputSOATest" />
	</wsdl:message>
	<wsdl:message name="outputSOATest">
		<wsdl:part name="out" element="tns:outputSOATest" />
	</wsdl:message>

<!--Define port definition -->
	<wsdl:portType name="SOATestEndpoint">
		<wsdl:operation name="SOATest">
			<wsdl:input message="tns:inputSOATest" />
			<wsdl:output message="tns:outputSOATest" />
		</wsdl:operation>
	</wsdl:portType>

<!--Bind Soap operation and service -->
	<wsdl:binding name="SOATestBinding" type="tns:SOATestEndpoint">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="SOATest">
			<soap:operation soapAction="http://javainuse.com"
				style="document" />
			<wsdl:input>
				<soap:body parts="in" use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body parts="out" use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>

<!--Define service -->
	<wsdl:service name="SOATestEndpointService">
		<wsdl:port name="SOATestEndpoint" binding="tns:SOATestBinding">
			<soap:address location="http://localhost:8181/cxf/javainuse/learn" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>


camel15_8
Next we define a cxfEndpoint and configure a route to expose webservice.
<?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:camel="http://camel.apache.org/schema/spring"
	xmlns:osgi="http://www.springframework.org/schema/osgi" 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://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.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-2.8.3.xsd">


	//define a cxfEndpoint for the wsdl
	<cxf:cxfEndpoint id="CXFTest" address="/javainuse/learn"
		endpointName="a:SOATestEndpoint" serviceName="a:SOATestEndpointService"
		wsdlURL="META-INF/wsdl/test.wsdl" serviceClass="com.javainuse.SOATestEndpoint"
		xmlns:a="http://javainuse.com" />

	<bean id="processor" class="com.javainuse.beans.CamelProcessor" />
	//camel route for processing the input
	<camelContext id="camelId" xmlns="http://camel.apache.org/schema/spring">

		<camel:route id="bookTicket">
			<camel:from uri="cxf:bean:CXFTest" />
			<process ref="processor" />
		</camel:route>

	</camelContext>
</beans>
package com.javainuse.beans;

import java.util.List;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import com.javainuse.InputSOATest;
import com.javainuse.OutputSOATest;

public class CamelProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {
    	
        OutputSOATest out = new OutputSOATest();
        
        //Get input from exchange
        List soaList = exchange.getIn().getBody(List.class);
        InputSOATest inputSOATest = (InputSOATest) soaList.get(0);
        out.setResult("Welcome " + inputSOATest.getTest().toString());
        //set output in exchange
        exchange.getOut().setBody(out);
    }

}

Start JBoss Fuse and type the command-
install mvn:com.javainuse/soapexample/0.0.1-SNAPSHOT
start 251
camel15_1
Use list command to check if the bundle is started correctly

camel15_2
Next go to url localhost:8181/cxf

camel15_3
Click on the wsdl hyperlink.
camel15_4
Open SOAPUI-
camel15_5

camel15_7

Download Source Code

Download it - Apache Camel +CXF SOAP Webservice Hello World

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions