CXF java2wsdl Example

CXF JavaToWS Tutorial explains about generating WSDL And Other Artifacts From Web Service Endpoint's Implementation (SEI) Using CXF JavaToWS Tool

I am showing here, an example of generating WSDL,XSD schema using CXF JavaToWS Tool.

You can also generate server side and client side code using this tool, you can go to below reference

Reference -> http://cxf.apache.org/docs/java-to-ws.html

Required Libraries

You need to download

  1. JDK 6
  2. Eclipse 3.7
  3. CXF-2.7.3

Following jar must be in ClassPath

  1. commons-collections-3.2.1.jar
  2. commons-lang-2.6.jar
  3. commons-logging-1.1.1.jar
  4. cxf-2.7.3.jar
  5. httpasyncclient-4.0-beta3.jar
  6. httpclient-4.2.1.jar
  7. httpcore-4.2.2.jar
  8. httpcore-nio-4.2.2.jar
  9. jaxb-impl-2.2.6.jar
  10. jaxb-xjc-2.2.6.jar
  11. neethi-3.0.2.jar
  12. velocity-1.7.jar
  13. wsdl4j-1.6.2.jar
  14. xmlschema-core-2.0.3.jar
Note

You can copy the implementation class and other domain objects from CXF Web Service Tutorial

(please see the below project structure screenshot)

CXF java2wsdl Example

JavaToWS Tutorial

I am going to call JavaToWS class from main method, you can also invoke JavaToWS from command line too.

Here I am providing option "-o" which specifies the name of the generated WSDL file, "-createxsdimports" specifying that seperated XSD will be generated and imported inside WSDL, if you are not specified this, complex types will be inlining them into the WSDL. And finally "-wsdl" which specifies to generate the WSDL file.

package com.tool;

import org.apache.cxf.tools.java2ws.JavaToWS;

//CXF java2wsdl Example
public class JavaToWSCodeGenerator {
 
public static void main(String[] args) {
   
try {
     
JavaToWS.main(new String[] { "-d", "src", "-o", "ChangeStudent.wsdl", "-createxsdimports", "-wsdl",
         
"com.student.ChangeStudentDetailsImpl" });
      System.out.println
("finished %%%%%%%%%%");
   
} catch (Exception e) {
     
e.printStackTrace();
   
}

  }

}

After running the above main class, you can see the generated WSDL and XSD schema on following screenshot

CXF java2wsdl Example

You can see the source of generated WSDL and XSD schema files below

ChangeStudent.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="ChangeStudentDetailsImplService"
	targetNamespace="http://student.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:tns="http://student.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
	<wsdl:types>
		<schema xmlns="http://www.w3.org/2001/XMLSchema">
			<import namespace="http://student.com/" schemaLocation="ChangeStudent_schema1.xsd" />
		</schema>
	</wsdl:types>
	<wsdl:message name="changeName">
		<wsdl:part name="parameters" element="tns:changeName">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="changeNameResponse">
		<wsdl:part name="parameters" element="tns:changeNameResponse">
		</wsdl:part>
	</wsdl:message>
	<wsdl:portType name="ChangeStudentDetails">
		<wsdl:operation name="changeName">
			<wsdl:input name="changeName" message="tns:changeName">
			</wsdl:input>
			<wsdl:output name="changeNameResponse" message="tns:changeNameResponse">
			</wsdl:output>
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="ChangeStudentDetailsImplServiceSoapBinding"
		type="tns:ChangeStudentDetails">
		<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="changeName">
			<soap:operation soapAction="" style="document" />
			<wsdl:input name="changeName">
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="changeNameResponse">
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="ChangeStudentDetailsImplService">
		<wsdl:port name="ChangeStudentDetailsImplPort" binding="tns:ChangeStudentDetailsImplServiceSoapBinding">
			<soap:address location="http://localhost:9090/ChangeStudentDetailsImplPort" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

ChangeStudent_schema1.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://student.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	attributeFormDefault="unqualified" elementFormDefault="unqualified"
	targetNamespace="http://student.com/">
	<xs:complexType name="student">
		<xs:sequence>
			<xs:element minOccurs="0" name="name" type="xs:string" />
		</xs:sequence>
	</xs:complexType>
	<xs:element name="changeName" type="changeName" />
	<xs:complexType name="changeName">
		<xs:sequence>
			<xs:element minOccurs="0" name="arg0" type="student" />
		</xs:sequence>
	</xs:complexType>
	<xs:element name="changeNameResponse" type="changeNameResponse" />
	<xs:complexType name="changeNameResponse">
		<xs:sequence>
			<xs:element minOccurs="0" name="return" type="student" />
		</xs:sequence>
	</xs:complexType>
</xs:schema>

 





Your email address will not be published. Required fields are marked *