Thursday, May 1, 2014

SOAP Webservice - JAX-WS


  1. Create a sample SOAP Webservice using JAX-WS
  2. Deploy in Tomcat 7.0
  3. Invoke WSDL
  4. Create Client
  5. Use Client to invoke Webservice

  1. Create a sample SOAP Webservice using JAX-WS
    1. Create a dynamic Web Project in Eclipse
    2. Create Service Endpoint Interface (SEI) HelloWorld 
    3. Create Service Inplementation Bean (SIB), HelloWorldImpl which implement SEI.
    4. Change web.xml, add entries if Servlet giving service to Webservice HelloWorldImpl 
    5. Add sun-jaxws.xml in WEB-INF folder. This file will publish and start the webservice runtime in Tomcat.
  2. Deploy in Tomcat 7.0
    1. Add a new server in Eclipse, pointing to Tomcat 7 installation directory.
    2. Add below jars in WEB-INF/lib folder
      1. gmbal-api-only.jar
      2. ha-api.jar
      3. jaxb-api.jar
      4. jaxb-impl-2.2.6.jar
      5. jaxb-impl.jar
      6. jaxws-rt.jar
      7. management-api.jar
      8. policy.jar
      9. stax-ex.jar
      10. streambuffer.jar
    3. Add the webservice project (created in 1) in server.
    4. Start the server.
  3. Invoke WSDL
    1. http://localhost:8080/SOAP_JAX/hello?wsdl
  4. Create Client
    1. Create a new java project in eclipse.
    2. Go to src directory in doc command prompt.
    3. Invoke command "wsimport -keep -verbose http://localhost:8080/SOAP_JAX/hello?wsdl"
    4. Two classes will be created in src folder. These are stubs to invoke webservice. 
    5. Write the client file using the stubs created in 4 above.
  5. Invoke main method to access webservice. (SEI)


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
    @WebResult(partName = "return")
    public String getHelloWorldAsString();

} (SIB)


import javax.jws.WebService;

@WebService(endpointInterface = "")
public class HelloWorldImpl implements HelloWorld {
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=""
<endpoint name="HelloWorld" implementation=""
url-pattern="/hello" />


WEB-INF/web.xml - setup servlet and url for webservice
<?xml version="1.0" encoding="UTF-8"?>


<display-name>Archetype Created Web Application</display-name>

<url-pattern>/hello</url-pattern>  <!-- service the webservice -->



<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at RI's version is JAX-WS RI 2.2.8 svn-revision#13980. --><!-- Generated by JAX-WS RI at RI's version is JAX-WS RI 2.2.8 svn-revision#13980. --><definitions xmlns:wsu="" xmlns:wsp="" xmlns:wsp1_2="" xmlns:wsam="" xmlns:soap="" xmlns:tns="" xmlns:xsd="" xmlns="" targetNamespace="" 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="" style="rpc"></soap:binding>
<operation name="getHelloWorldAsString">
<soap:operation soapAction=""></soap:operation>
<soap:body use="literal" namespace="http://com.soap.sanjeev/"></soap:body>
<soap:body use="literal" namespace="http://com.soap.sanjeev/"></soap:body>
<service name="HelloWorldImplService">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
<soap:address location="http://localhost:8080/SOAP_JAX/hello"></soap:address>

</definitions>   -  Webservice Client 

package com.client.soap.jaxws.sanjeev;



public class HelloClient {
    static HelloWorldImplService service;

    public static void main(String[] args) {
        try {
            HelloClient client = new HelloClient();
        } catch(Exception e) {

    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();
        } catch(Exception e) {


No comments:

Post a Comment

Amazon Best Sellors

Weblogic - Server Creation - Issues

Datasource creation using python script.  Python Script was failing after creating Datasource. From UI Admin Console, if I try to activat...