Create a SOAP web service and test in SOAP UI






1] Create WSServiceHello Endpoint Interface:

WSServiceHello.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package com.dineshonjava.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface WSServiceHello {
 @WebMethod
 String sayHello(String name);
}



2] Create WSServiceHello Endpoint Implementation class:
WSServiceHelloImpl.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package com.dineshonjava.ws;

import javax.jws.WebService;

@WebService(endpointInterface = "com.dineshonjava.ws.WSServiceHello")
public class WSServiceHelloImpl implements WSServiceHello {
 @Override
 public String sayHello(String name) {
  return "Hello, Welcome to JAX-WS World and keep learning!! " + name;
 }
}



3] Create Endpoint Publisher class:
WSHelloPublisher.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.dineshonjava.publisher;

import javax.xml.ws.Endpoint;
import com.dineshonjava.ws.WSServiceHelloImpl;

public class WSHelloPublisher {
 // you can use any port no. 9090 or 9191 etc
 public static void main(String[] args) {
  Endpoint.publish("http://localhost:9090/WS/WSServiceHello", new WSServiceHelloImpl());
 }
}

/*

1]access wsdl file by typing the following in url
http://localhost:9090/WS/WSServiceHello?wsdl


2]References:
http://www.dineshonjava.com/2013/06/jax-ws-hello-world-example.html
and
http://java.dzone.com/articles/jax-ws-hello-world
(Both are same program)

3]Search for 'service name' and 'portType name' in wsdl file

(1)While creating client, create object of 'service name' ie. <service name="WSServiceHelloImplService">
(2)Also create object of 'port name' ie. <portType name="WSServiceHello">

*/


4] Run WSHelloPublisher.java program.Your web service is published.
    You can check your wsdl at
    http://localhost:9090/WS/WSServiceHello?wsdl


Developing WebService Client 
1] we need to generate the client stubs.
- Create a new project in eclipse.
- Open your command line, and enter the wsimport command in cmd:
CD <path of new project>\src  
wsimport -keep http://localhost:9090/WS/WSServiceHello?wsdl  

Click the image to zoom


you will find java classes generated and compiled under src->com->dineshonjava->ws

Note: You may face the following error
wsimport is not recognized as an internal command
Soln: Follow these steps
Update your PATH variable
1)Go to My computer >> right click and select properties.
2)On the properties tab select Advanced system settings
3)Click enviroment variables
4)select path and click edit option
  add
  ";C:\Program Files\Java\jdk1.7.0_60\bin"
  at the end.
5)close the cmd and try again.
Done.


2] Lets create client class now.
create WSHelloClient.java under src->com.dineshonjava.client

WSHelloClient.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package com.dineshonjava.client;

import com.dineshonjava.ws.WSServiceHello;
import com.dineshonjava.ws.WSServiceHelloImplService;

public class WSHelloClient {

 public static void main(String[] args) {
  WSServiceHelloImplService service = new WSServiceHelloImplService();
  WSServiceHello serviceHello = service.getWSServiceHelloImplPort();
  System.out.println("***************Call Started*******************");
  System.out.println(serviceHello.sayHello("Rahul "));
  System.out.println("***************Call Ended*********************");
 }

}


Output:
3] Run above program and you will get the output in console.
***************Call Started*******************
Hello, Welcome to JAX-WS World and keep learning!! Rahul
***************Call Ended*********************

Now Webservice is started and is ready to be tested.


Test in SOAP UI
1) Open soap ui.
2) Right click project and click new SOAP project.

Click the image to zoom


















Click the image to zoom
















3) Give url of wsdl file 

4) Double click Request 1, give input and run the program


Click the image to zoom




















Note: Output of your SOAP, Client class and Publisher class is same

No comments:

Post a Comment