- Create a sample SOAP Webservice using JAX-WS
- Deploy in Tomcat 7.0
- Invoke WSDL
- Create Client
- Use Client to invoke Webservice
- Create a sample SOAP Webservice using JAX-WS
- Create a dynamic Web Project in Eclipse
- Create Service Endpoint Interface (SEI) HelloWorld
- Create Service Inplementation Bean (SIB), HelloWorldImpl which implement SEI.
- Change web.xml, add entries if Servlet giving service to Webservice HelloWorldImpl
- Add sun-jaxws.xml in WEB-INF folder. This file will publish and start the webservice runtime in Tomcat.
- Deploy in Tomcat 7.0
- Add a new server in Eclipse, pointing to Tomcat 7 installation directory.
- Add below jars in WEB-INF/lib folder
- gmbal-api-only.jar
- ha-api.jar
- jaxb-api.jar
- jaxb-impl-2.2.6.jar
- jaxb-impl.jar
- jaxws-rt.jar
- management-api.jar
- policy.jar
- stax-ex.jar
- streambuffer.jar
- Add the webservice project (created in 1) in server.
- Start the server.
- Invoke WSDL
- http://localhost:8080/SOAP_JAX/hello?wsdl
- Create Client
- Create a new java project in eclipse.
- Go to src directory in doc command prompt.
- Invoke command "wsimport -keep -verbose http://localhost:8080/SOAP_JAX/hello?wsdl"
- Two classes will be created in src folder. These are stubs to invoke webservice.
- Write the client file using the stubs created in 4 above.
- Invoke main method to access webservice.
HelloWorld.java (SEI)
package com.ws.soap.jaxws.sanjeev;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.4-b01
* Generated source version: 2.2
*
*/
@WebService(name = "HelloWorld", targetNamespace = "http://com.soap.sanjeev/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWorld {
/**
*
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(partName = "return")
public String getHelloWorldAsString();
}
package com.ws.soap.jaxws.sanjeev;
import javax.jws.WebService;
@WebService(endpointInterface = "com.ws.soap.jaxws.sanjeev.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String getHelloWorldAsString() {
// TODO Auto-generated method stub
return "hello world";
}
}
WEB-INF/sun-jaxws.xml - will publish the webservice and start the webservice runtime
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="HelloWorld" implementation="com.ws.soap.jaxws.sanjeev.HelloWorldImpl"
url-pattern="/hello" />
</endpoints>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="HelloWorld" implementation="com.ws.soap.jaxws.sanjeev.HelloWorldImpl"
url-pattern="/hello" />
</endpoints>
WEB-INF/web.xml - setup servlet and url for webservice
<?xml version="1.0" encoding="UTF-8"?>
<web-app
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"
version="2.4">
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern> <!-- service the webservice -->
</servlet-mapping>
</web-app>
WSDL File
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.8 svn-revision#13980. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.8 svn-revision#13980. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://sanjeev.jaxws.soap.ws.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://sanjeev.jaxws.soap.ws.com/" name="HelloWorldImplService">
<import namespace="http://com.soap.sanjeev/" location="http://localhost:8080/SOAP_JAX/hello?wsdl=1"></import>
<binding xmlns:ns1="http://com.soap.sanjeev/" name="HelloWorldImplPortBinding" type="ns1:HelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"></soap:binding>
<operation name="getHelloWorldAsString">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="http://com.soap.sanjeev/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="http://com.soap.sanjeev/"></soap:body>
</output>
</operation>
</binding>
<service name="HelloWorldImplService">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
<soap:address location="http://localhost:8080/SOAP_JAX/hello"></soap:address>
</port>
</service>
</definitions>
HelloClient.java - Webservice Client
package com.client.soap.jaxws.sanjeev;
import javax.xml.ws.WebServiceRef;
import com.ws.soap.jaxws.sanjeev.HelloWorld;
import com.ws.soap.jaxws.sanjeev.HelloWorldImplService;
public class HelloClient {
@WebServiceRef(wsdlLocation="http://localhost:8080/SOAP_JAX/hello?wsdl")
static HelloWorldImplService service;
public static void main(String[] args) {
try {
HelloClient client = new HelloClient();
client.doTest(args);
} catch(Exception e) {
e.printStackTrace();
}
}
public void doTest(String[] args) {
try {
service = new HelloWorldImplService();
System.out.println("Retrieving the port from the following service: " + service);
HelloWorld port = service.getHelloWorldImplPort();
System.out.println("Invoking the sayHello operation on the port.");
String name;
if (args.length > 0) {
name = args[0];
} else {
name = "No Name";
}
String response = port.getHelloWorldAsString();
System.out.println(response);
} catch(Exception e) {
e.printStackTrace();
}
}
}
<web-app
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"
version="2.4">
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern> <!-- service the webservice -->
</servlet-mapping>
</web-app>
WSDL File
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.8 svn-revision#13980. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.8 svn-revision#13980. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://sanjeev.jaxws.soap.ws.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://sanjeev.jaxws.soap.ws.com/" name="HelloWorldImplService">
<import namespace="http://com.soap.sanjeev/" location="http://localhost:8080/SOAP_JAX/hello?wsdl=1"></import>
<binding xmlns:ns1="http://com.soap.sanjeev/" name="HelloWorldImplPortBinding" type="ns1:HelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"></soap:binding>
<operation name="getHelloWorldAsString">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="http://com.soap.sanjeev/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="http://com.soap.sanjeev/"></soap:body>
</output>
</operation>
</binding>
<service name="HelloWorldImplService">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
<soap:address location="http://localhost:8080/SOAP_JAX/hello"></soap:address>
</port>
</service>
</definitions>
HelloClient.java - Webservice Client
package com.client.soap.jaxws.sanjeev;
import javax.xml.ws.WebServiceRef;
import com.ws.soap.jaxws.sanjeev.HelloWorld;
import com.ws.soap.jaxws.sanjeev.HelloWorldImplService;
public class HelloClient {
@WebServiceRef(wsdlLocation="http://localhost:8080/SOAP_JAX/hello?wsdl")
static HelloWorldImplService service;
public static void main(String[] args) {
try {
HelloClient client = new HelloClient();
client.doTest(args);
} catch(Exception e) {
e.printStackTrace();
}
}
public void doTest(String[] args) {
try {
service = new HelloWorldImplService();
System.out.println("Retrieving the port from the following service: " + service);
HelloWorld port = service.getHelloWorldImplPort();
System.out.println("Invoking the sayHello operation on the port.");
String name;
if (args.length > 0) {
name = args[0];
} else {
name = "No Name";
}
String response = port.getHelloWorldAsString();
System.out.println(response);
} catch(Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment