JAX-WS (Java API for XML Web Services) technology can be use to develop both web services & clients which communicates via XML. So all the calls and responses are transmitted as SOAP messages (XML files). Here I'm going to develop my servicewith netbeans IDE.
Creating a Web Service
- Select file -> new project from the menu and choose the project category as java web -> web application.
- Name the project as "WSApplication" then select the server appropriately. Here i used GlassFish server.
- After creating the project we are going to create the web service. First right click on the project and select New -> Web Service. Then name the service and select a location or the package. According to the my sample, name the service as MultiplicationService and the package name as jaxws.
- Now we are going to implement our service. The class MultiplicationService is annotated with @WebService annotation as the web service endpoint and it declares a simple method multiplication, with @WebMethod annotation (to exposed the the method to the client). Finally the method returns the multiplication of given two parameters.
- Now you can deploy your service and run it. Right click on your project and select deploy, then you can run your application.
- Using wsimport command we can generate all the JAX-WS artifacts by importing the, http://localhost:8080/WSApplication/MultiplicationServiceService?WSDL
- Open the command prompt and import artifacts in to a new package. First move to the java directory in the netbeans project. > cd WSApplication\src\java
- So this is the new view of the project window
package jaxws.multiplication; import javax.jws.WebService; import javax.jws.WebMethod; @WebService public class MultiplicationService { @WebMethod public int multiplication(int i, int j) { return i * j; } }
> wsimport -p soap -keep http://localhost:8080/WSApplication/MultiplicationServiceService?WSDL
- -p -> Specifying a target package
- -keep -> Keep generated files
Creating the Client
After importing JAX-WS artifacts, it's an easy task to create a client for the multiplication service by using MultiplicationService and MultiplicationServiceService classes (generated by the wsimport command). Code for the sample client is given below. It is giving the result of the multiplication method.
package jaxwsclient; import soap.MultiplicationServiceService; import soap.MultiplicationService; public class Client { public static void main(String[] args) { MultiplicationServiceService service = new MultiplicationServiceService(); MultiplicationService port = service.getMultiplicationServicePort(); System.out.println(port.multiplication(5, 2)); } }
So it's all about creating a simple client for a jaxws web service. Hope this will be useful for u all....