`
jaesonchen
  • 浏览: 309747 次
  • 来自: ...
社区版块
存档分类
最新评论

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

 
阅读更多

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

Following the official guide, at the chapter 19 we’ll find the Spring support about web service (honestly, could it miss?).

In this article I’ll describe the use of it with Jax-WS standard (introduced in Java EE 5 and Java 6) and we’ll see how build and consume the web service.

 

First of all, you have to notice that there are a huge variety of methods about build and consume web service. I think I’ve never used two times the same method for building web services. I don’t know why, probably that is the evidence about the large number of technologies for building web services.

My personal opinion is to use technologies which implements java standard, like Jax-WS.

In this first part we’ll take a look about build web service. I’m an old style developer and I’m not able to start from the WSDL definition. So, my approach is to define the interface with methods exposed rather than define the WSDL definition.

1
2
3
4
5
6
7
8
9
10
package it.springjaxws;
public interface OrderService {
  
 public void Check(Order order) throws Exception;
 
 public Order Process(Order order);
 
 public Order Shipping(Order order);
 
}

And its implementation:

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
package it.springjaxws;
import java.util.Date;
 
public class OrderServiceImpl implements OrderService{
 
 @Override
 public void Check(Order order) throws Exception{
  if (order.getItemNumber()==0)
  {
   throw new Exception("Quantity cannot be 0!!");
  }  
 }
 
 @Override
 public Order Process(Order order) {
  order.setInvoice(order.getItemNumber() * 1.3);
  return order;
 }
 
 @Override
 public Order Shipping(Order order) {
  order.setShippingDate(new Date());
  return order;
 }
 
}

The Order bean:

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
63
64
65
66
67
68
69
70
71
package it.springjaxws;
import java.io.Serializable;
import java.util.Date;
 
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement(name="Order")
public class Order implements Serializable {
 
 /**
  *
  */
 private static final long serialVersionUID = 1L;
  
  
 private String orderId;
  
 private double invoice;
  
 private int itemNumber;
 
 private Date shippingDate;
  
 public Order()
 {}
  
 public Order(String orderId, double invoice, int itemNumber)
 {
  this.orderId = orderId;
  this.invoice = invoice;
  this.itemNumber = itemNumber;    
 }
  
 public String getOrderId() {
  return orderId;
 }
 public void setOrderId(String orderId) {
  this.orderId = orderId;
 }
 public double getInvoice() {
  return invoice;
 }
 public void setInvoice(double invoice) {
  this.invoice = invoice;
 }
  
 public int getItemNumber() {
  return itemNumber;
 }
 public void setItemNumber(int itemNumber) {
  this.itemNumber = itemNumber;
 }
 
 public Date getShippingDate() {
  return shippingDate;
 }
 
 public void setShippingDate(Date shippingDate) {
  this.shippingDate = shippingDate;
 }
  
 @Override
 public String toString()
 {
  return " orderId:" + orderId +
    " itemNumber:" + itemNumber +
    " invoice:" + invoice +
    " shippingDate:" + shippingDate;
 }
  
}

Now we have to expose the class “OrderService” by an endpoint. For this achieve we need to use a class which extends SpringBeanAutowiringSupport spring’s class. Take a look at the code.

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
package it.springjaxws;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
 
@WebService(serviceName="OrderService")
public class OrderServiceEndpoint extends SpringBeanAutowiringSupport{
 
 @Autowired
 private OrderService orderService;
  
 @WebMethod
 public void Check(@WebParam(name = "order") Order order) throws Exception
 {
  orderService.Check(order);
 }
  
 @WebMethod
 public Order Process(@WebParam(name = "order") Order order)
 {
  return orderService.Process(order);
 }
 
 @WebMethod
 public Order Shipping(@WebParam(name = "order") Order order)
 {
  return orderService.Shipping(order);
 }
}

Briefly, we’ve mapped the orderService methods with the WebMethod.

The spring configuration file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 
    <!-- Use @Component annotations for bean definitions -->
    <context:component-scan base-package="it.springjaxws"/>
 
    <bean id="orderService" class="it.springjaxws.OrderServiceImpl" />
 
    <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
        <property name="baseAddress" value="http://localhost:8081/"/>
    </bean>
 
    <bean id="orderServiceEndpoint" class="it.springjaxws.OrderServiceEndpoint" />
 
</beans>

We expose the web service at port 8081 of localhost.

Deploy it to your tomcat and run. You’ll see something like above code when you’ll browse /OrderServiceEndpoint?WSDL

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net.
    RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net.
    RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://springjaxws.it/"
    name="OrderService">
    <types>
        <xsd:schema>
            <xsd:import namespace="http://springjaxws.it/"
                schemaLocation="http://localhost:8081/OrderServiceEndpoint?xsd=1"></xsd:import>
        </xsd:schema>
    </types>
    <message name="Check">
        <part name="parameters" element="tns:Check"></part>
    </message>
    <message name="CheckResponse">
        <part name="parameters" element="tns:CheckResponse"></part>
    </message>
    <message name="Exception">
        <part name="fault" element="tns:Exception"></part>
    </message>
    <message name="Process">
        <part name="parameters" element="tns:Process"></part>
    </message>
    <message name="ProcessResponse">
        <part name="parameters" element="tns:ProcessResponse"></part>
    </message>
    <message name="Shipping">
        <part name="parameters" element="tns:Shipping"></part>
    </message>
    <message name="ShippingResponse">
        <part name="parameters" element="tns:ShippingResponse"></part>
    </message>
    <portType name="OrderServiceEndpoint">
        <operation name="Check">
            <input message="tns:Check"></input>
            <output message="tns:CheckResponse"></output>
            <fault message="tns:Exception" name="Exception"></fault>
        </operation>
        <operation name="Process">
            <input message="tns:Process"></input>
            <output message="tns:ProcessResponse"></output>
        </operation>
        <operation name="Shipping">
            <input message="tns:Shipping"></input>
            <output message="tns:ShippingResponse"></output>
        </operation>
    </portType>
    <binding name="OrderServiceEndpointPortBinding" type="tns:OrderServiceEndpoint">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
            style="document"></soap:binding>
        <operation name="Check">
            <soap:operation soapAction=""></soap:operation>
            <input>
                <soap:body use="literal"></soap:body>
            </input>
            <output>
                <soap:body use="literal"></soap:body>
            </output>
            <fault name="Exception">
                <soap:fault name="Exception" use="literal"></soap:fault>
            </fault>
        </operation>
        <operation name="Process">
            <soap:operation soapAction=""></soap:operation>
            <input>
                <soap:body use="literal"></soap:body>
            </input>
            <output>
                <soap:body use="literal"></soap:body>
            </output>
        </operation>
        <operation name="Shipping">
            <soap:operation soapAction=""></soap:operation>
            <input>
                <soap:body use="literal"></soap:body>
            </input>
            <output>
                <soap:body use="literal"></soap:body>
            </output>
        </operation>
    </binding>
    <service name="OrderService">
        <port name="OrderServiceEndpointPort" binding="tns:OrderServiceEndpointPortBinding">
            <soap:address location="http://localhost:8081/OrderServiceEndpoint"></soap:address>
        </port>
    </service>
</definitions>

In the next part I describe how consume it.

UPDATE

You can find very useful to host the web services in the same web server port (the 8080 for tomcat). For this purpose you have to use JAX-WS commons from Metro project of glassfish. More info are available at this url http://jax-ws-commons.java.net/spring/

To apply this technology at the previous example, we need to change and update some files.

First, in web.xml, we put the servlet definition

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
<web-app id="WebApp_ID" version="2.4"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 
    <display-name>SpringJaxWsEmbedded</display-name>
 
    <servlet>
        <servlet-name>jaxwsembedded</servlet-name>
        <servlet-class>
            com.sun.xml.ws.transport.http.servlet.WSSpringServlet
        </servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>jaxwsembedded</servlet-name>
        <url-pattern>/order</url-pattern>
    </servlet-mapping>
 
    <!-- Register Spring Listener -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
 
</web-app>

After this, we edit the applicationContext.xml file

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
<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 
<!-- Use @Component annotations for bean definitions -->
    <context:component-scan base-package="it.springjaxwsembedded" />
 
    <bean id="orderService" class="it.springjaxws.OrderServiceImpl" />
 
    <wss:binding url="/order">
        <wss:service>
            <ws:service bean="#orderWs" />
        </wss:service>
    </wss:binding>
 
    <!-- Web service methods -->
    <bean id="orderWs" class="it.springjaxws.OrderServiceEndpoint" />
 
</beans>

Last point is OrderServiceEndpoint definition

1
2
3
public class OrderServiceEndpoint {
...
}

We’ve removed the SpringBeanAutowiringSupport extension.

分享到:
评论

相关推荐

    jaxb-api.jar.jaxws-api.zip_ jaxb-api.jar_cxf_jax-ws.jar_jaxb-api

    它支持基于标准的服务实现,如JAX-WS(Java API for XML Web Services)和JAX-RS(Java API for RESTful Web Services)。CXF不仅提供了服务端的实现,还支持客户端调用,使得开发者可以方便地创建、发布和管理Web...

    JAX-WS 2.2 完整jar包

    FastInoset.jar gmbal-api-only.jar ha-api.jar javax.annotation.jar javax.mail_1.4.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jaxws-api.jar jaxws-rt.jar jaxws-rt-javadoc.jar ...

    Jax-ws所需要的JAR包

    Java API for XML Web Services(JAX-WS)是Java平台上用于构建和消费Web服务的标准API。它简化了创建和使用Web服务的过程,使得开发者能够通过SOAP消息与远程服务进行交互。JAX-WS允许开发者将服务接口直接映射到...

    JAX-WS 2.2 RI所有相关jar包

    JAX-WS 2.2 RI 所包含的JAR包集合,包含25个JAR包,列表如下: FastInoset.jar gmbal-api-only.jar ha-api.jar javax.annotation.jar javax.mail_1.4.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jaxws-api...

    JAX-WS所需Jar包

    JAX-WS(Java API for XML Web Services)是Java平台上的一个标准,用于构建和部署Web服务。这个标准允许开发人员使用简单的编程模型来创建和消费Web服务,从而简化了分布式系统间的交互。在Java环境中,JAX-WS提供...

    jax-ws api jar包

    JAX-WS(Java API for XML Web Services)是Java平台标准版(Java SE)和企业版(Java EE)的一部分,它为创建、部署和消费基于SOAP(Simple Object Access Protocol)的Web服务提供了全面的支持。JAX-WS允许开发者...

    jax-0.4.13.tar.gz

    `jax-0.4.13.tar.gz` 是一个开源库 JAX 的特定版本,即版本 0.4.13 的压缩包文件。JAX 是一个由 Google Brain 团队开发的高性能计算库,专为科学计算和深度学习任务设计,尤其在机器学习研究领域广泛应用。JAX 提供...

    jax-ws2.1.zip

    1. **webservices-api.jar** - 这个JAR文件包含了JAX-WS的API接口定义,它定义了开发者用来创建和消费Web服务的类和接口。这些接口包括`javax.jws.WebService`注解,用于标记服务端点,以及`javax.xml.ws.Service`,...

    Jax-ws RI.zip

    Java API for XML Web Services (JAX-WS) 是Java平台上的一个标准,用于构建和部署Web服务。JAX-WS RI(Reference Implementation)是这个规范的官方参考实现,它提供了开发、测试和运行基于SOAP(Simple Object ...

    jax-rs jax-ws所需包,亲测可用

    标题"jax-rs jax-ws所需包,亲测可用"表明这个压缩包包含了用于开发Java RESTful Web服务(JAX-RS)和Java SOAP Web服务(JAX-WS)所需的库文件。这些库是Java应用程序进行Web服务交互的核心组件,确保了对HTTP协议...

    JAX-WS所需要的JAR包

    JAX-WS(Java API for XML Web Services)是Java平台上的一个标准,用于构建和部署Web服务。它提供了一种基于标准的、类型安全的方式来创建和消费SOAP消息。JAX-WS通过Java SE和Java EE环境下的API,使得开发者能够...

    jax-ws webservice demo

    基于jax-ws 实现的web service client和server端的demo程序。 注:如果使用的是 myeclipse 时 server 部署到tomcat 启动的时候会报错 解决办法:找到myeclipse安装目录下的 plugins 目录里 查找 webservices-rt.jar,...

    JAX-WS.zip

    Java Architecture for XML Binding (JAX-WS) 是Java平台上的一个标准组件,用于处理Web服务。它提供了在Java应用程序和Web服务之间交换XML数据的能力。JAX-WS允许开发者创建符合WS-I Basic Profile的Web服务,确保...

    jax-ws-2.2.rar

    在Tomcat上发布webservice所需要的jar包,总共25个:FastInfoset.jar/gmbal-api-only.jar/ha-api.jar/javax.annotation.jar/javax.mail_1.4.jar/jaxb-api.jar/jaxb-impl.jar/jaxb-xjc.jar/jaxws-api.jar/jaxws-rt....

    解决weblogic部署JAX-WS需要的配置文件

    JAX-WS(Java API for XML Web Services)是Java平台上的一个标准,用于创建和部署Web服务。WebLogic作为一款强大的Java EE应用服务器,支持JAX-WS标准,但正确配置和部署这些服务需要一些额外的步骤。本指南将详细...

    jax-0.3.25-py3-none-any.whl

    文件格式:whl安装步骤:切换到whl路径执行pip install [whl文件名]注意whl对应python版本

    JAX-WS + Spring 实现webService示例.docx

    在本文中,我们将深入探讨如何使用JAX-WS(Java API for XML Web Services)与Spring框架集成来实现一个Web服务示例。这个示例将帮助我们理解如何在Spring环境中创建、配置和部署JAX-WS服务,同时利用Spring的依赖...

    metro-jax-ws-jaxws221x.zip

    【标题】"metro-jax-ws-jaxws221x.zip" 提供的是一个关于JAX-WS(Java API for XML Web Services)的开发示例,其中包含了JAX-WS 2.2.1版本的相关组件和库文件。这个压缩包是针对Java开发者设计的,用于帮助他们理解...

Global site tag (gtag.js) - Google Analytics