Remote Procedure Calls With JAX-WS

Web Services

In simple terms, a web services are client server applications that communicate over the internet using HTTP. They provide interoperability between different software applications that run on different platforms and frameworks.

According to Oracle, There are two types of web services:

1. “Big” Web Services.

The “Big” web services uses Simple Object Access Protocol (SOAP), an XML language which standardize the message architecture and message formats. These services often contains a machine-readable description of the operations offered by the service, written in the Web Services Description Language(WSDL) which is also an XML language used for syntactically defining interfaces. JAX-WS provides the functionality for “Big” web services in Java EE 6.

2. RESTful Web Services

Representational State Transfer web services or RESTful web services are integrated with HTTP rather than SOAP and does not require XML messages or WSDL service–API definitions. Due to the fact that RESTful services use existing well-known W3C standards, they have a lightweight infrastructure and can be built with minimal tooling. JAX-RS provides the functionality for RESTful services in Java EE 6.

Jakarta XML Web Services (JAX-WS)

JAX-WS is the Java API for XML Web Services. It allows the developers to create both message oriented and Remote Procedure Call-oriented (RPC-oriented) web services. The main benefit of using JAX-WS API is that it hides the complexity of the SOAP messages from the application developer.

JAX-WS uses annotations which are introduced in Java SE 5 to simply the development and deployment of web services and clients. Also, since it is a part of the standard Java API, the developers do not need to load any additional JAR files to work with it.

Now that we have a basic understanding about web services and JAX-WS, let’s get our hands dirty!

Building a Hello World web service with JAX-WS (RPC style)

We need the following java files for our hello world service.

HelloWorld.java

An interface for declaring the methods that are exposed in the hello world web service.

package se.jax_ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld
{
@WebMethod
String getHelloWorldAsString(String name);
}

HelloWorldImpl.java

Implementation of the methods declared in the HelloWorld interface are done in this class.

package se.jax_ws;

import javax.jws.WebService;

//Service Implementation
@WebService(endpointInterface = "se.jax_ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
@Override
public String getHelloWorldAsString(String name)
{
return "Hello from JAX-WS " + name;
}
}

Notice that we have provide our HelloWorld interface to the endpointInterface in WebService annotation. Because of that, JAX-WS know the class to use for implementation when web service methods are invoked.

HelloWorldPublisher.java

This class publishes our web service via the JAX-WS Endpoint.

package se.jax_ws;

import javax.xml.ws.Endpoint;

//Endpoint publisher
public class HelloWorldPublisher
{
public static void main(String[] args)
{
Endpoint.publish("http://localhost:9999/ws/hello",
new HelloWorldImpl());
}
}

HelloWorldClient.java

Client program for invoking our hello world service.

package se.jax_ws;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class HelloWorldClient
{
public static void main(String[] args) throws Exception
{
URL url = new URL("http://localhost:9999/ws/hello?wsdl");

//1st argument service URI, refer to wsdl document above
//2nd argument is service name, refer to wsdl document above
QName qname = new QName("http://jax_ws.se/",
"HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.getHelloWorldAsString("John Doe"));
}
}

When we run the HelloWorldClient after publishing the service, we get the following output.

Hello from JAX-WS John Doe

When we look at the client code, we can see that service.getPort() method returns a proxy (usually called the stub) which represents the remote procedure code. When the remote procedure is called, the proxy forwards the request to the JAX-WS runtime in the local computer. JAX-WS runtime takes care of the communication with the remote server and its JAX-WS runtime using the SOAP messages.

That’s all for this article. I’ll see you on the next one! 👋

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store