JAX-WS web services
Build a SOAP web service with JAX-WS: generate artifacts and WSDL with wsgen, publish an endpoint, and generate a client with wsimport.
3 min readUpdated
Java has two standard APIs for web services: JAX-WS (Java API for XML Web Services, now Jakarta XML Web Services) for SOAP services described by a WSDL file, and JAX-RS (now Jakarta RESTful Web Services) for REST services over plain HTTP. This note covers JAX-WS: a minimal service class, the wsgen tool that generates its deployment artifacts and WSDL, publishing it with Endpoint.publish, and the wsimport tool that generates a client from the published WSDL.
Note: wsgen and wsimport shipped in $JDK/bin up to Java 10. Java 11 removed JAX-WS from the JDK; on current Java, get the tools and the runtime from the Jakarta XML Web Services reference implementation (Eclipse Metro), and use the jakarta.jws and jakarta.xml.ws packages instead of javax.jws and javax.xml.ws. The examples below use the original javax names.
Write the service class
A JAX-WS service is a plain class annotated with @WebService; each @WebMethod becomes a SOAP operation. This one returns 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"; }}Generate server artifacts with wsgen
wsgen reads a compiled service implementation class and generates the portable JAX-WS artifacts needed to deploy it: a request and a response wrapper class for each operation. Run it from the directory that holds the compiled classes:
wsgen -verbose -keep -cp . com.mkyong.ws.ServerInfo-keep keeps the generated source files next to the compiled classes, and -cp sets where to find the service class. For ServerInfo 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 also generate the WSDL file and its XML schema, add -wsdl:
wsgen -verbose -keep -cp . com.mkyong.ws.ServerInfo -wsdlThis time it generates 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.wsdlCurrent JAX-WS runtimes can create the wrapper classes on the fly when the service is published, so wsgen is mostly useful to get the WSDL file ahead of time.
Publish the endpoint
Endpoint.publish starts a lightweight HTTP server inside the JVM and publishes the service at the given address, which is enough for testing without an application server:
package com.mkyong.endpoint;
import javax.xml.ws.Endpoint;import com.mkyong.ws.ServerInfo;
public class WsPublisher {
public static void main(String[] args) { Endpoint.publish("http://localhost:8888/ws/server", new ServerInfo()); System.out.println("Service is published!"); }}While it runs, the WSDL is served at http://localhost:8888/ws/server?wsdl.
Generate a client with wsimport
wsimport parses a published WSDL file and generates the portable artifacts a client needs to call the service:
wsimport -keep -verbose http://localhost:8888/ws/server?wsdlFor the service above it prints:
parsing WSDL...
generating code...com/mkyong/ws/GetIpAddress.javacom/mkyong/ws/GetIpAddressResponse.javacom/mkyong/ws/ObjectFactory.javacom/mkyong/ws/ServerInfo.javacom/mkyong/ws/ServerInfoService.javacom/mkyong/ws/package-info.javaThe class to work with is ServerInfoService, the one annotated with @WebServiceClient. ServerInfo is the generated service interface (port type).
@WebServiceClient(name = "ServerInfoService", targetNamespace = "http://ws.mkyong.com/", wsdlLocation = "http://localhost:8888/ws/server?wsdl")public class ServerInfoService extends Service { // ...}A client creates the service, gets a port (a proxy for the remote service) and calls the operation like a local method:
public class WsClient {
public static void main(String[] args) throws Exception { ServerInfoService sis = new ServerInfoService(); ServerInfo si = sis.getServerInfoPort(); System.out.println(si.getIpAddress()); }}References
- Jakarta XML Web Services specification
- Eclipse Metro JAX-WS reference implementation
- JEP 320: Remove the Java EE and CORBA modules
- JAX-WS wsgen tool example (Mkyong)
- JAX-WS wsimport tool example (Mkyong)
- JAX-WS SOAP handler on the server side (Mkyong)
- JAX-WS SOAP handler on the client side (Mkyong)
- Container authentication with JAX-WS on Tomcat (Mkyong)