JAX-WS Client: Consuming the JAX-WS webservice!

In this post we’ll see how to write a client to consume the JAX-WS webservices. This client will invoke the methods of a remote webservice. For this we’ll utilize the already created JAX-WS webservice in this post.

Since, it’s the era of webapps, our client too will be powered by Spring MVC! We can make the client in two ways, and each is simpler than the other!

Download the Client’s Source Code!

PizzaStory
This is what we’re here to do! Order Pizza! 🙂

This picture pretty much sums up the design, architecture and structure of this Producer-Consumer code!

Let’s start!

1. Download and import the PizzaService first, and get it up and running. Confirm by seeing the wsdl file at this location:

http://localhost:8080/webservice-jaxws/PizzaService?wsdl

2. Generate the artifacts using the wsimport tool. This tool will generate the required classes for you from the wsdl file. These artifacts are necessary, because now your client doesn’t know what a Pizza object looks like, and it doesn’t know which methods he can call on the remote webservice, let alone the location of it!

3. Open the command prompt and type in the following command to generate the artifacts.  Before running the command, create two folders “src” and “gen” to have the source and class files generated at different locations.

wsimport -s src -d gen http://localhost:8080/webservice-jaxws/PizzaService?wsdl

The command and its explanation!
The command and its explanation!

The directory structure to be followed is as follows:-

dir

4. Go in the “src” directory, and see the generated artifacts for yourself! I’ve included a screen-shot too! I’m so dilligent 🙂

The prized artifacts!
The prized artifacts!

5. The next thing is to copy these in your client application. I’ve written a small Spring MVC project for this purpose, and included the above classes in it. It’s hosted on Google codes and you can download it!

6. Sending the request via Ajax! We’ll write this piece of code in the form.jsp page, and on click of a button, this ajax request would be sent to the controller which in turn would call the webservices!


<script type="text/javascript">
 $(function() {

$('#invokeWebservice').click(function() {

//Sending the Ajax request, to call the JAX-WS service.
 $.ajax({
 url : '<%=request.getContextPath()%>/invokeWebservice',
 dataType: 'json',
 success: function(data){

//Setting the data in the page, for viewing!
 $('#pizzaSize').html(data.size);
 $('#pizzaCost').html(data.cost);
 $('#pizzaDateOfManufacture').html(data.dateOfManufacture);
 }
 });
 });
 });
</script>

7. Invoking the webservice can be done in two ways:-

a) Using the generated proxy classes by wsimport tool.

This is the controller. The other parts have been omitted for brevity, and only the code concerned with invoking the webservice is shown here. You can see the complete code by downloading the project! Don’t fret! 🙂

@Controller
@RequestMapping("/")
public class ClientController {

 //This gives us a proxy to the remote webservice.
 //Now call the methods on local "pizzaProxy"
 //and the remote ones will be called automatically!
 private PizzaServiceImpl pizzaProxy = new PizzaService().getPizzaPort();

//This handler will be invoked on the Ajax request.
 @RequestMapping("/invokeWebservice")
 public @ResponseBody Pizza getPizza() {
 //The webservice is being invoked by "pizzaProxy"
 //This method returns a Pizza object.
 return pizzaProxy.getPizza();
 }

}

b) Using Spring’s JaxWsPortProxyFactoryBean

For using the JaxWsPortProxyFactoryBean, you need to configure this in the Spring’s WebApplicationContext file.

<bean id="pizzaProxy" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
 <property name="serviceInterface" value="webservice.jaxws.generated.PizzaServiceImpl"/>
 <property name="wsdlDocumentUrl" value="http://localhost:8080/webservice-jaxws/PizzaService?wsdl"/>
 <property name="namespaceUri" value="generated.jaxws.webservice"/>
 <property name="serviceName" value="PizzaService"/>
 <property name="portName" value="PizzaPort"/>
 </bean>

Then autowire this in the Controller!

@Autowired
 @Qualifier("pizzaProxy")
 private PizzaServiceImpl pizzaProxy;

The directory structure of the client project is as shown below:-
projstructure
Run the the client and you’ll see the following screens.
screen1Click the button to invoke the webservice. And you’ll see the response!
screen2
That’s all for this one! Happy consuming 🙂

5 thoughts on “JAX-WS Client: Consuming the JAX-WS webservice!”

      1. Hi Richard,

        I’m afraid what your doubt is! If you’re wondering how the object would be written in XML, then it happens behind-the-scenes and JAXWS automatically takes care of it for you. All you need to do is annotate your service properly with @Webservice and then it should work.

        On Tue, Jun 3, 2014 at 7:58 PM, Ankeet Maini wrote:

        >

        Like

Leave a comment