Skip to content

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:

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";
}
}

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:

Terminal window
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.java
com/mkyong/ws/jaxws/GetIpAddress.class
com/mkyong/ws/jaxws/GetIpAddressResponse.java
com/mkyong/ws/jaxws/GetIpAddressResponse.class

To also generate the WSDL file and its XML schema, add -wsdl:

Terminal window
wsgen -verbose -keep -cp . com.mkyong.ws.ServerInfo -wsdl

This time it generates 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

Current 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:

WsPublisher.java
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:

Terminal window
wsimport -keep -verbose http://localhost:8888/ws/server?wsdl

For the service above it prints:

Terminal window
parsing WSDL...
generating code...
com/mkyong/ws/GetIpAddress.java
com/mkyong/ws/GetIpAddressResponse.java
com/mkyong/ws/ObjectFactory.java
com/mkyong/ws/ServerInfo.java
com/mkyong/ws/ServerInfoService.java
com/mkyong/ws/package-info.java

The 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:

WsClient.java
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

Related notes

2 min read

gRPC

What gRPC is, how it differs from JSON over REST, and its four kinds of RPC with a Protocol Buffers service example.

  • APIs & Integration
  • Architecture & Process
3 min read

Strava

Upload GPS tracks to Strava from the command line with curl: create an API application, get an OAuth token and call the uploads endpoint.

  • APIs & Integration
2 min read

Read Google Sheets as JSON

Read a public Google Sheet as JSON over HTTP, with the keyless gviz endpoint or the Sheets API v4 and an API key.

  • APIs & Integration

Type to search the whole site.