Web Service
April 30, 2015

References

Web Service

  • JAX-WS represents SOAP (Simple Object Access Protocol)
  • JAX-RS represents REST (Representational State Transfer )

JAX-WS

wsimport tool

The wsimport tool is used to parse the published service WSDL (Web Services Description Language) file (http://localhost:8888/ws/server?wsdl) and generate required files (JAX-WS portable artifacts) for web service client to access the published web services. The wsimport tool is available in the $JDK/bin folder.

1
$ wsimport -keep -verbose http://localhost:8888/ws/server?wsdl
1
2
3
4
5
6
7
8
9
parsing WSDL...

generating code...
com\mkyong\ws\GetServerName.java
com\mkyong\ws\GetServerNameResponse.java
com\mkyong\ws\ObjectFactory.java
com\mkyong\ws\ServerInfo.java
com\mkyong\ws\ServerInfoService.java
com\mkyong\ws\package-info.java

You may only need to concern on the ServerInfoService.java having @WebServiceClient annotation.

1
2
3
4
5
6
7
@WebServiceClient(name = "ServerInfoService",
	targetNamespace = "http://ws.mkyong.com/",
	wsdlLocation = "http://localhost:8888/ws/server?wsdl")
public class ServerInfoService extends Service
{
	//......
}

A client to access the published service.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class WsClient {

	public static void main(String[] args) throws Exception {

		ServerInfoService sis = new ServerInfoService();
		ServerInfo si = sis.getServerInfoPort();

		System.out.println(si.getServerName());

  }

}

wsgen tool

The wsgen tool is used to parse an existing web service implementation class and generates required files (JAX-WS portable artifacts) for web service deployment. This wsgen tool is available in $JDK/bin folder.

Let’s see a web service implementation class, quite simple, just a method to return a string.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// ServerInfo.java
package com.mkyong.ws;

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

@WebService
public class ServerInfo{

	@WebMethod
	public String getIpAddress() {

		return "10.10.10.10";

	}

}

Below code will generate JAX-WS portable artifacts (Java files) for web service deployment.

1
$ wsgen -verbose -keep -cp . com.mkyong.ws.ServerInfo

In this case, it generates four files :

1
2
3
4
com\mkyong\ws\jaxws\GetIpAddress.java
com\mkyong\ws\jaxws\GetIpAddress.class
com\mkyong\ws\jaxws\GetIpAddressResponse.java
com\mkyong\ws\jaxws\GetIpAddressResponse.class

To generate WSDL and xsd files for above web service implementation class (ServerInfo.java), add an extra -wsdl in the wsgen command :

1
$ wsgen -verbose -keep -cp . com.mkyong.ws.ServerInfo -wsdl

In this case, it generated six files :

1
2
3
4
5
6
com\mkyong\ws\jaxws\GetIpAddress.java
com\mkyong\ws\jaxws\GetIpAddress.class
com\mkyong\ws\jaxws\GetIpAddressResponse.java
com\mkyong\ws\jaxws\GetIpAddressResponse.class
ServerInfoService_schema1.xsd
ServerInfoService.wsdl

Now all files are ready, publish it via endpoint publisher.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.mkyong.endpoint;

import javax.xml.ws.Endpoint;
import com.mkyong.ws.ServerInfo;

//Endpoint publisher
public class WsPublisher{

	public static void main(String[] args) {
	   Endpoint.publish("http://localhost:8888/ws/server", new ServerInfo());

	   System.out.println("Service is published!");
    }

}

JAX-RS