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

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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.

1
2
3
4
5
6
7
8
9
< 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值