본문 바로가기

2021/July 2021

How SOAP works in Java! (feat JAX-WS

SOAP uses JAX-WS as a middleware on the Application Layer, which provides a servlet class. 
Treat this as a follow up to Servlet post.

 

SOAP is Simple Object Access Protocol. It runs on application layer, wrapping HTTP protocol. SOAP doesn't fully use the funcionalities of HTTP. Rather than request/response based interaction, it is usually used to fascilitate Remote Procedure Call (RPC).

 

What we essentially want to achieve through SOAP is:

1. we want to host a service (in our case it would be HelloWorldImpl that implements HelloWorld interface) on the server.

2. we want to access that service from the client. By that, we mean we literally want to be able to invoke a method from HelloWorld interface from the client.

 

We want to host a service

First create an interface

SOAP binding makes this web service a SOAP (host an object, access object). RPC is a reiteration of what we are trying to achieve (remote procedure call).

 

Then we make a concrete implementation of that interface:

Full name of HelloWorld interface is se325.example04.server.HelloWorld

'endpoint' would just be the full class name of the interface it implements.

 

This is it! If you run this main function, you have HelloWorldImpl instance hosted at

You can check it by visiting that website:

http://localhost:10000/ws/hello

We are not using any servlet containers like Tomcat. Instead, we are just running it on JAX-WS, which is just a middleware that fascilitates these server stuff.

 

VERY IMPORTANT:
Above image shows 'endpoint'. This is where HelloWorldImpl that have been hosted lives as a service. There's a lot of complicated bindings that results in creation of this service (refer to WDSL section way below). We are going to need this address in the client code so wait for it!

 

 

We want to access the service

This is what we need:

Let's break it down one by one:

This is the URL endpoint where we can access our web service.

 

However, our service itself lives on 

This!

QName is an object that represents address to that specific service.

 

Now we get the service instance:

But this is a Service instance. We need a HelloWorld instance, and this can be done by requesting an object of the type HelloWorld:

Then we can just invoke method as we would on a regular java object. But the client themselves don't even have an implementation of that method declared! 

So we are grabbing the object hosted on the server, assign it to the variable helloService. Then we simply invoke a method on it, and it would produce effects as if that HelloWorldImpl actually existed in the client, when in reality, it was in the server. Really cool stuff!

 

 

WDSL (web service description language)

The best way to understand what goes under it is to go deeper.

 

 

Recall that after we ran below server code:

 
http://localhost:10000/ws/hello

We could visit here. http://localhost:10000/ws/hello?wsdl is where that WSDL link points to.

 

WSDL is a web service description language, which is simply XML file that describes what services HelloWorldImpl provides.

http://localhost:10000/ws/hello?wsdl

Here's a rough mapping of Service codes to WDSL

Service code to WSDL

And mapping of Client code to WDSL

 

Client code to WSDL

This is the folder structure: