Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Tuesday, 31 December 2013

JAX-WS Hello World Web service – RPC Style

In this tutorial we are going to see how to create web service end point and client in RPC style SOAP binding . Follow the steps given below to create RPC style SOAP web service
1. Open Eclipse, Select File –> New  --> Java Project
2. Create Java Project called WebService
3. Create package com.javatutorialscorner.jaxws.helloworld under WebService
4. Create Interface HelloWorld under package com.javatutorialscorner.jaxws.helloworld
HelloWorld.java
01.package com.javatutorialscorner.jaxws.helloworld;
02. 
03.import javax.jws.WebMethod;
04.import javax.jws.WebService;
05.import javax.jws.soap.SOAPBinding;
06.import javax.jws.soap.SOAPBinding.Style;
07. 
08.@WebService
09.@SOAPBinding(style = Style.RPC)
10.public interface HelloWorld {
11.   @WebMethod
12.  public String sayHello(String message);
13.}

5. Create End Point Implementation class HelloWorldImpl under package com.javatutorialscorner.jaxws.helloworld

HelloWorldImpl.java

01.package com.javatutorialscorner.jaxws.helloworld;
02. 
03.import javax.jws.WebService;
04. 
05.@WebService(endpointInterface = "com.javatutorialscorner.jaxws.helloworld.HelloWorld")
06.public class HelloWorldImpl implements HelloWorld {
07. 
08.  @Override
09.   public String sayHello(String message) {
10.        // TODO Auto-generated method stub
11.      return "Java Tutorials Corner Says " + message;
12. }
13. 
14.}

6.Create End point publisher class HelloWorldPublisher under package com.javatutorialscorner.jaxws.helloworld

HelloWorldPublisher .java

01.package com.javatutorialscorner.jaxws.helloworld;
02. 
03.import javax.xml.ws.Endpoint;
04. 
05.public class HelloWorldPublisher {
06. 
07.    public static void main(String[] args) {
08.        // TODO Auto-generated method stub
09.      Endpoint.publish("http://localhost:9080/helloworld",
10.                new HelloWorldImpl());
11.  }
12. 
13.}

7.Now run the end point publisher class . Now your web service is published in the following URL

http://localhost:9080/helloworld

if you run the URL in web browser you can see the following screen

web-service

8.You can test the deployed web service by accessing the generated WSDL file via http://localhost:9080/helloworld?wsdl this URL

Generated WDSL file

01.<?xml version="1.0" encoding="UTF-8"?>
02.<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
03.<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
05.<types/>
06.<message name="sayHello">
07.<part name="arg0" type="xsd:string"/>
08.</message>
09.<message name="sayHelloResponse">
10.<part name="return" type="xsd:string"/>
11.</message>
12.<portType name="HelloWorld">
13.<operation name="sayHello">
15.<output wsam:Action="http://helloworld.jaxws.javatutorialscorner.com/HelloWorld/sayHelloResponse" message="tns:sayHelloResponse"/>
16.</operation>
17.</portType>
18.<binding name="HelloWorldImplPortBinding" type="tns:HelloWorld">
19.<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
20.<operation name="sayHello">
21.<soap:operation soapAction=""/>
22.<input>
23.<soap:body use="literal" namespace="http://helloworld.jaxws.javatutorialscorner.com/"/>
24.</input>
25.<output>
26.<soap:body use="literal" namespace="http://helloworld.jaxws.javatutorialscorner.com/"/>
27.</output>
28.</operation>
29.</binding>
30.<service name="HelloWorldImplService">
31.<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
32.<soap:address location="http://localhost:9080/helloworld"/>
33.</port>
34.</service>
35.</definitions>

9.You can access the web service in following way

  • Access the web service using Java Client

  • Access the web service using wsimport tool
Access the web service using Java Client

1. Create New Java Project called WebServiceClient

2.Create package com.javatutorialscorner.jaxws.client under WebServiceClient

3. Create client class HelloWorldClient under com.javatutorialscorner.jaxws.client

HelloWorldClient.java

01.package com.javatutorialscorner.jaxws.client;
02. 
03.import java.net.MalformedURLException;
04.import java.net.URL;
05. 
06.import javax.xml.namespace.QName;
07.import javax.xml.ws.Service;
08. 
09.import com.javatutorialscorner.jaxws.helloworld.HelloWorld;
10. 
11.public class HelloWorldClient {
12. 
13.  /**
14.  * @param args
15.   */
16. public static void main(String[] args) {
17.        // TODO Auto-generated method stub
18.      try {
19.           URL url = new URL("http://localhost:9080/helloworld?wsdl");
20.         QName qName = new QName(
21.                    "http://helloworld.jaxws.javatutorialscorner.com/",
22.                 "HelloWorldImplService");
23.           Service service = Service.create(url, qName);
24.           HelloWorld helloWorld = service.getPort(HelloWorld.class);
25.          System.out.println("Web Service Message "
26.                   + helloWorld.sayHello("Hello"));
27.        } catch (MalformedURLException e) {
28.         // TODO Auto-generated catch block
29.          e.printStackTrace();
30.        }
31. 
32.   }
33. 
34.}

4. Run the client see the following output in console

Web Service Message Java Tutorials Corner Says Hello

HelloWorldClient.java

Access the web service using wsimport tool

1. Create New Java Project called WebServiceClient

2. Generate client stubs by running the following commads

1.cd %project_home%/src wsimport -s . http://localhost:9080/helloworld?wsdl

or

1.cd %project_home%/src wsimport -keep http://localhost:9080/helloworld?wsdl

3. You can see the one classes and one interfaces generated in WebServiceClient project’s com.javatutorialscorner.jaxws.helloworld package.i.e

  1. interface HelloWorld.java

  2. class HelloWorldImplService.java
 HelloWorld.java

01.package com.javatutorialscorner.jaxws.helloworld;
02. 
03.import javax.jws.WebMethod;
04.import javax.jws.WebParam;
05.import javax.jws.WebResult;
06.import javax.jws.WebService;
07.import javax.jws.soap.SOAPBinding;
08. 
09. 
10./**
11. * This class was generated by the JAXWS SI.
12. * JAX-WS RI 2.0_02-b08-fcs
13. * Generated source version: 2.0
14. *
15. */
16.@WebService(name = "HelloWorld", targetNamespace = "http://helloworld.jaxws.javatutorialscorner.com/")
17.@SOAPBinding(style = SOAPBinding.Style.RPC)
18.public interface HelloWorld {
19. 
20. 
21.    /**
22.     *
23.     * @param arg0
24.     * @return
25.     *     returns java.lang.String
26.     */
27.    @WebMethod
28.    @WebResult(partName = "return")
29.    public String sayHello(
30.        @WebParam(name = "arg0", partName = "arg0")
31.        String arg0);
32. 
33.}

HelloWorldImplService.java

01.package com.javatutorialscorner.jaxws.helloworld;
02. 
03.import java.net.MalformedURLException;
04.import java.net.URL;
05.import javax.xml.namespace.QName;
06.import javax.xml.ws.Service;
07.import javax.xml.ws.WebEndpoint;
08.import javax.xml.ws.WebServiceClient;
09. 
10. 
11./**
12. * This class was generated by the JAXWS SI.
13. * JAX-WS RI 2.0_02-b08-fcs
14. * Generated source version: 2.0
15. *
16. */
17.@WebServiceClient(name = "HelloWorldImplService", targetNamespace = "http://helloworld.jaxws.javatutorialscorner.com/", wsdlLocation = "http://localhost:9080/helloworld?wsdl")
18.public class HelloWorldImplService
19.    extends Service
20.{
21. 
22.    private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
23. 
24.    static {
25.        URL url = null;
26.        try {
27.            url = new URL("http://localhost:9080/helloworld?wsdl");
28.        } catch (MalformedURLException e) {
29.            e.printStackTrace();
30.        }
31.        HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url;
32.    }
33. 
34.    public HelloWorldImplService(URL wsdlLocation, QName serviceName) {
35.        super(wsdlLocation, serviceName);
36.    }
37. 
38.    public HelloWorldImplService() {
39.        super(HELLOWORLDIMPLSERVICE_WSDL_LOCATION, new QName("http://helloworld.jaxws.javatutorialscorner.com/", "HelloWorldImplService"));
40.    }
41. 
42.    /**
43.     *
44.     * @return
45.     *     returns HelloWorld
46.     */
47.    @WebEndpoint(name = "HelloWorldImplPort")
48.    public HelloWorld getHelloWorldImplPort() {
49.        return (HelloWorld)super.getPort(new QName("http://helloworld.jaxws.javatutorialscorner.com/", "HelloWorldImplPort"), HelloWorld.class);
50.    }
51. 
52.}

4. Create client class HelloWorldClient under com.javatutorialscorner.jaxws.client to access the generated service

HelloWorldClient.java

01.package com.javatutorialscorner.jaxws.client;
02. 
03.import com.javatutorialscorner.jaxws.helloworld.HelloWorld;
04.import com.javatutorialscorner.jaxws.helloworld.HelloWorldImplService;
05. 
06.public class HelloWorldClient {
07.   public static void main(String[] args) {
08.        HelloWorldImplService service = new HelloWorldImplService();
09.        HelloWorld helloWorld = service.getHelloWorldImplPort();
10.        System.out.println("Web Service message : "
11.             + helloWorld.sayHello("Hello"));
12.    }
13.}

5. Run the client see the following output in console


Web Service message : Java Tutorials Corner Says Hello

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Axis2 – Message exchange patternIn this Tutorial we are going to see the quick overview of SOAP  message exchange patterns. Message Exchange PatternAll the SOAP messages carry the same structure, the ways in which they are used can be combined into a … Read More
  • SOAP Web service Introduction In this tutorial we are going to see the introduction about SAOP web service. SOAP – Simple Object Access Protocol is standard protocol specification for message exchange based on XML.  Web service as two component … Read More
  • JAX-WS wsimport tool In this tutorial we are going to see how to generate client using wsimport tool The wsimpot tool is used to generate generate web service client from WSDL file to access the published web service. you can see the wsimport to… Read More
  • JAX-WS wsgen tool In this tutorial we are going to see how to generate web service client using wsgen tool. The wsgen tool is used to generate required files from web service implementation class for web service deployment. The wsgen tool is … Read More
  • JAX-WS Hello World Web serviceIn this tutorial we are going to see how to create web service end point and client. Default its document style SOAP binding. Follow the steps given below to create hello world SOAP web service. 1. Open Eclipse, Select Fil… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JAX-WS Hello World Web service – RPC Style Rating: 5 Reviewed By: eHowToNow