Web Service
SOAP web services with JAX-WS: publishing endpoints, WSDL and generating clients with wsimport.
2 min read
References
- JAX-WS : SOAP handler in client side
- JAX-WS : wsimport tool example
- JAX-WS : wsgen tool example
- Container Authentication with JAX-WS
- JAX-WS : SOAP handler in server side
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?wsdlparsing WSDL...
generating code...com\mkyong\ws\GetServerName.javacom\mkyong\ws\GetServerNameResponse.javacom\mkyong\ws\ObjectFactory.javacom\mkyong\ws\ServerInfo.javacom\mkyong\ws\ServerInfoService.javacom\mkyong\ws\package-info.javaYou 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.
package com.mkyong.ws;
import javax.jws.WebMethod;import javax.jws.WebService;
@WebServicepublic 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.ServerInfoIn this case, it generates four files :
com\mkyong\ws\jaxws\GetIpAddress.javacom\mkyong\ws\jaxws\GetIpAddress.classcom\mkyong\ws\jaxws\GetIpAddressResponse.javacom\mkyong\ws\jaxws\GetIpAddressResponse.classTo 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 -wsdlIn this case, it generated six files :
com\mkyong\ws\jaxws\GetIpAddress.javacom\mkyong\ws\jaxws\GetIpAddress.classcom\mkyong\ws\jaxws\GetIpAddressResponse.javacom\mkyong\ws\jaxws\GetIpAddressResponse.classServerInfoService_schema1.xsdServerInfoService.wsdlNow all files are ready, publish it via endpoint publisher.
package com.mkyong.endpoint;
import javax.xml.ws.Endpoint;import com.mkyong.ws.ServerInfo;
//Endpoint publisherpublic class WsPublisher{
public static void main(String[] args) { Endpoint.publish("http://localhost:8888/ws/server", new ServerInfo());
System.out.println("Service is published!"); }
}