Spring Jax-Ws. Build and consume Web Services – part 2

In the previous article we’ve seen how build a Jax Web Services with spring. Now it’s time to see how consume it always with spring.

Obviously we can consume every types of web services with this technology, not only the spring web services.

Starting from WSDL definition you can use wsimport tool for generating the code:


wsimport -s c:\src http://localhost:8081/OrderServiceEndpoint?WSDL "-target 2.0
parsing WSDL...
Generating code...
Compiling code...

For my purpose, I need only two files: OrderServiceEndpoint.java (rename in OrderService.java) and Order.java.

The first file contains the service interface mapped with Jax annotations.


package it.springjaxws;

import javax.jws.WebParam;
import javax.jws.WebService;

import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 *
 */
@WebService(name = "OrderServiceEndpoint", targetNamespace = "http://springjaxws.it/")
public interface OrderService {

    /**
     *
     * @param order
     * @throws Exception_Exception
     */
    @WebMethod(operationName = "Check")
    @RequestWrapper(localName = "Check", targetNamespace = "http://springjaxws.it/", className = "it.springjaxws.Check")
    @ResponseWrapper(localName = "CheckResponse", targetNamespace = "http://springjaxws.it/", className = "it.springjaxws.CheckResponse")
    public void check(
        @WebParam(name = "order", targetNamespace = "")
        Order order)
        throws Exception
    ;

    /**
     *
     * @param order
     * @return
     *     returns it.springjaxws.Order
     */
    @WebMethod(operationName = "Process")
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "Process", targetNamespace = "http://springjaxws.it/", className = "it.springjaxws.Process")
    @ResponseWrapper(localName = "ProcessResponse", targetNamespace = "http://springjaxws.it/", className = "it.springjaxws.ProcessResponse")
    public Order process(
        @WebParam(name = "order", targetNamespace = "")
        Order order);

    /**
     *
     * @param order
     * @return
     *     returns it.springjaxws.Order
     */
    @WebMethod(operationName = "Shipping")
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "Shipping", targetNamespace = "http://springjaxws.it/", className = "it.springjaxws.Shipping")
    @ResponseWrapper(localName = "ShippingResponse", targetNamespace = "http://springjaxws.it/", className = "it.springjaxws.ShippingResponse")
    public Order shipping(
        @WebParam(name = "order", targetNamespace = "")
        Order order);

}

The order file class is the same used in server part.

The spring configuration file contains the binding with the Web Service.

<bean id="orderWebService"
	class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
	<property name="serviceInterface" value="it.springjaxws.OrderService" />
	<property name="wsdlDocumentUrl"
		value="http://localhost:8081/OrderServiceEndpoint?WSDL" />
	<property name="namespaceUri" value="http://springjaxws.it/" />
	<property name="serviceName" value="OrderService" />
	<property name="portName" value="OrderServiceEndpointPort" />
</bean>

I used Spring Mvc to call the Web Services. This is the controller class.


package it.consumespringjaxws.controller;

import it.springjaxws.Order;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class OrderController {
 
 @Autowired
 private it.springjaxws.OrderService orderClient;
 
 Logger log = Logger.getLogger(this.getClass());
 
 @RequestMapping(value="/order")
 public String getOrder(ModelMap model) {

  Order order = new Order();
  order.setOrderId("CK-1244");
  order.setItemNumber(5);
  
  try {
   orderClient.check(order);
  } catch (Exception e) {
   log.error(e);
  }
  log.info("******************************************");
  order = orderClient.process(order);
  log.info("******************************************");
  order = orderClient.shipping(order);
  
  model.addAttribute("detail", order);
  return "order";

 }
}

The result is in the below screenshot.

Summary

I think that spring is a good implementation of Jax-Ws reference. Personally, this is my prefer approach for building and consuming web services. That’s because you need a very few configuration options and you can expose all Java class file as service.

Advertisements

2 thoughts on “Spring Jax-Ws. Build and consume Web Services – part 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s