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.

wsimport -keep -verbose http://localhost:8888/ws/server?wsdl
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.

@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.

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.

// 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.

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

In this case, it generates four files :

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 :

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

In this case, it generated six files :

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.

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