`
rogerhunt
  • 浏览: 60267 次
  • 性别: Icon_minigender_1
  • 来自: 新加坡
社区版块
存档分类
最新评论

Web Services and JAX-WS

阅读更多
  • Web Services and JAX-WS

In recent years, web services have emerged as a popular technology for remote method calls. Technically, a web service has two components:

A service that can be accessed with the SOAP transport protocol

A description of the service in the WSDL format

SOAP is an XML protocol for invoking remote methods, similar to the protocol that RMI uses for the communication between clients and servers. Just as you can program RMI applications without knowing anything about the details of the RMI protocol, you don't really need to know any details about SOAP to call a web service.

WSDL is an interface description language. It too is based on XML. A WSDL document describes the interface of a web service: the methods that can be called, and their parameter and return types. In this section, we generate a WSDL document from a service implemented in Java. This document contains all the information that a client program needs to invoke the service, whether it is written in Java or another programming language.

  • Using JAX-WS

There are several toolkits for implementing web services in Java. In this section, we discuss the JAX-WS technology that is included in Java SE 6 and above.

With JAX-WS, you do not provide an interface for a web service. Instead, you annotate a class with @WebService, as shown in Listing 10-13. Note also the @WebParam annotation of the description parameter. It gives the parameter a humanly readable name in the WSDL file. (This annotation is optional. By default, the parameter would be called arg0.)

Listing 10-13. Warehouse.java
引用
Code View:
1. package com.horstmann.corejava;
2. import java.util.*;
3. import javax.jws.*;
4.
5. /**
6.  * This class is the implementation for a Warehouse web service
7.  * @version 1.0 2007-10-09
8.  * @author Cay Horstmann
9.  */
10.
11. @WebService
12. public class Warehouse
13. {
14.    public Warehouse()
15.    {
16.       prices = new HashMap<String, Double>();
17.       prices.put("Blackwell Toaster", 24.95);
18.       prices.put("ZapXpress Microwave Oven", 49.95);
19.    }
20.
21.    public double getPrice(@WebParam(name="description") String description)
22.    {
23.       Double price = prices.get(description);
24.       return price == null ? 0 : price;
25.    }
26.
27.    private Map<String, Double> prices;
28. }




In RMI, the stub classes were generated dynamically, but with JAX-WS, you run a tool to generate them. Change to the base directory of the Webservices1 source and run the wsgen class as follows:
引用

wsgen -classpath . com.horstmann.corejava.Warehouse

引用

Note

  The wsgen tool requires that the class that provides the web service is contained in a package other than the default package.


The tool generates two rather mundane classes in the com.horstmann.corejava.jaxws package. The first class encapsulates all parameters of the call:

Code View:
引用
public class GetPrice
{
    private String description;
    public String getDescription() { return this.description; }
    public void setDescription(String description) { this.description = description; }
}


 


The second class encapsulates the return value:

引用
public class GetPriceResponse
{
    private double _return;
    public double get_return() { return this._return; }
    public void set_return(double _return) { this._return = _return; }
}


Typically, one has a sophisticated server infrastructure for deploying web services, which we do not discuss here. The JDK contains a very simple mechanism for testing a service. Simply call the Endpoint.publish method. A server is started on the given URL—see Listing 10-14.

Listing 10-14. WarehouseServer.java
引用
Code View:
1. package com.horstmann.corejava;
2.
3. import javax.xml.ws.*;
4.
5. public class WarehouseServer
6. {
7.    public static void main(String[] args)
8.    {
9.       Endpoint.publish("http://localhost:8080/WebServices/warehouse", new Warehouse());
10.    }
11. }


At this point, you should compile the server classes, run wsgen, and start the server:

java com.horstmann.corejava.WarehouseServer


Now point your web browser to http://localhost:8080/WebServices/warehouse?wsdl. You will get this WSDL file:

Code View:
引用
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://corejava.horstmann.com/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  targetNamespace="http://corejava.horstmann.com/" name="WarehouseService">
  <types>
    <xsd:schema>
      <xsd:import schemaLocation="http://localhost:8080/WebServices/warehouse?xsd=1"
      namespace="http://corejava.horstmann.com/"></xsd:import>
    </xsd:schema>
  </types>
  <message name="getPrice">
    <part element="tns:getPrice" name="parameters"></part>
  </message>
  <message name="getPriceResponse">
    <part element="tns:getPriceResponse" name="parameters"></part>
  </message>
  <portType name="Warehouse">
    <operation name="getPrice">
      <input message="tns:getPrice"></input>
      <output message="tns:getPriceResponse"></output>
    </operation>
  </portType>
  <binding name="WarehousePortBinding" type="tns:Warehouse">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
    <operation name="getPrice">
      <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="WarehouseService">
    <port name="WarehousePort" binding="tns:WarehousePortBinding">
      <soap:address location="http://localhost:8080/WebServices/warehouse"></soap:address>
    </port>
  </service>
</definitions>


 


This description tells us that an operation getPrice is provided. Its input is a tns:getPrice and its output is a tns:getPriceResponse. (Here, tns is the namespace alias for the target namespace, http://corejava.horstmann.com.)

To understand these types, point your browser to http://localhost:8080/WebServices/warehouse?xsd=1. You will get this XSL document:

Code View:
引用
<xs:schema targetNamespace="http://corejava.horstmann.com/" version="1.0">
   <xs:element name="getPrice" type="tns:getPrice"/>
   <xs:element name="getPriceResponse" type="tns:getPriceResponse"/>
   <xs:complexType name="getPrice">
      <xs:sequence><xs:element name="description" type="xs:string" minOccurs="0"/></xs:sequence>
   </xs:complexType>
   <xs:complexType name="getPriceResponse">
      <xs:sequence><xs:element name="return" type="xs:double"/></xs:sequence>
   </xs:complexType>
</xs:schema>


Now you can see that getPrice has a description element of type string, and getPriceResponse has a return element of type double.
引用

Note

  The WSDL file does not specify what the service does. It only specifies the parameter and return types.

  • A Web Service Client

Let's turn to implementing the client. Keep in mind that the client knows nothing about the server except what is contained in the WSDL. To generate Java classes that can communicate with the server, you generate a set of client classes, using the wsimport utility.

引用
Code View:
wsimport -keep -p com.horstmann.corejava.server http://localhost:8080/WebServices/warehouse?wsdl


 


The -keep option keeps the source files, in case you want to look at them. The following classes and interfaces are generated:

引用
GetPrice
GetPriceResponse
Warehouse
WarehouseService
ObjectFactory



You already saw the GetPrice and GetPriceResponse classes.

The Warehouse interface defines the remote getPrice method:

引用
Code View:
public interface Warehouse
{
    @WebMethod public double getPrice(@WebParam(name = "description") String description);
}


 


You only need to know one thing about the WarehouseService class: its getPort method yields a stub of type Warehouse through which you invoke the service—see Listing 10-15.

You can ignore the ObjectFactory class as well as the file package-info.java that defines a package-level annotation. (We discuss annotations in detail in Chapter 11.)

Note

  You can use any convenient package for the generated classes. If you look closely, you will notice that the GetPrice and GetPriceResponse classes are in different packages on the server and client. This is not a problem. After all, neither the server nor the client know about each other's Java implementation. They don't even know whether the other is implemented in Java.





Listing 10-15. WarehouseClient.java
引用
Code View:
1. import java.rmi.*;
2. import javax.naming.*;
3. import com.horstmann.corejava.server.*;
4.
5. /**
6.  * The client for the warehouse program.
7.  * @version 1.0 2007-10-09
8.  * @author Cay Horstmann
9.  */
10. public class WarehouseClient
11. {
12.    public static void main(String[] args) throws NamingException, RemoteException
13.    {
14.       WarehouseService service = new WarehouseService();
15.       Warehouse port = service.getPort(Warehouse.class);
16.
17.       String descr = "Blackwell Toaster";
18.       double price = port.getPrice(descr);
19.       System.out.println(descr + ": " + price);
20.    }
21. }


 




Now you are ready to run the client program. Double-check that the server is still running, open another shell window, and execute

引用
java WarehouseClient



You will get the familiar message about the price of a toaster.

Note

  You might wonder why there is no equivalent of a RMI registry. When you locate a remote object for RMI, the client need not know on which server the object is located. It merely needs to know how to locate the registry. However, to make a web service call, the client needs the URL of the server. It is hardwired into the WarehouseService class.





We used a network sniffer to see how the client and server actually communicate (see Figure 10-8). The client sends the following request to the server:

引用
Code View:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://corejava.horstmann.com/">
   <soapenv:Body>
      <ns1:getPrice><description>Blackwell Toaster</description></ns1:getPrice>
   </soapenv:Body>
</soapenv:Envelope>


 



Figure 10-8. Analyzing SOAP traffic

[View full size image]





The server responds:

引用
Code View:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://corejava.horstmann.com/">
   <soapenv:Body>
      <ns1:getPriceResponse><return>24.95</return></ns1:getPriceResponse>
   </soapenv:Body>
</soapenv:Envelope>


 


In this section, you have seen the essentials about web services:
  • The services are defined in a WSDL document, which is formatted as XML.
  • The actual request and response methods use SOAP, another XML format.
  • Clients and servers can be written in any language.















分享到:
评论

相关推荐

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

    2. **JAX-WS**(Java API for XML Web Services)是Java平台上的SOAP(Simple Object Access Protocol)Web服务标准,主要用于创建面向服务的架构(SOA)。JAX-WS提供了处理XML消息、WSDL(Web服务描述语言)和UDDI...

    Jax-ws所需要的JAR包

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

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

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

    JAX-WS所需Jar包

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

    jax-ws api jar包

    - **可扩展性**:支持WS-Security、WS-ReliableMessaging等高级Web服务协议,以满足安全性、可靠性的需求。 5. **与JAX-RPC的区别:** - JAX-WS是JAX-RPC的后续版本,它在设计上更注重简洁性和性能,且更符合现代...

    一个包含jax-ws和jax-rs的例子(含服务端和客户端)

    标题中的“一个包含jax-ws和jax-rs的例子(含服务端和客户端)”是指这是一个示例项目,它演示了如何使用Java API for XML Web Services (JAX-WS)和Java API for RESTful Web Services (JAX-RS)来创建和消费Web服务。...

    jax-ws webservice demo

    注:如果使用的是 myeclipse 时 server 部署到tomcat 启动的时候会报错 解决办法:找到myeclipse安装目录下的 plugins 目录里 查找 webservices-rt.jar,然后将webservices-rt.jar 外层的 lib目录里删除,或者备份的...

    JAX-WS API, JAX-RS API

    Java API for XML Web Services (JAX-WS) 是Java平台上的一个标准接口,用于创建和消费Web服务。它是Sun Microsystems在2004年推出的一个重要框架,旨在简化Web服务的开发,使得Java开发者能够更方便地实现基于SOAP...

    jax-ws2.1.zip

    2. **webservices-extra.jar** - 此文件可能包含JAX-WS的一些扩展或额外功能,如WS-I(Web Services Interoperability)支持,或者对特定Web服务协议的实现,例如WS-Security(Web Services Security)或其他厂商...

    JAX-WS2.0 API

    JAX-WS(Java API for XML Web Services)2.0 API 是Java平台上的一个标准接口,用于创建和消费Web服务。它提供了一种简单、类型安全且与平台无关的方式来实现基于SOAP(Simple Object Access Protocol)的Web服务。...

    webservice之jax-ws

    开发者可以使用诸如Apache CXF、Metro等开源框架来支持JAX-WS的开发,这些框架提供了更多的功能,如WS-Security、WS-Policy等扩展,同时简化了开发过程。 8. **示例** 在提供的链接...

    jax-ws发布webservice

    在"jax-ws-client"目录中,包含了调用上述Web服务的客户端代码。创建JAX-WS客户端通常包括以下步骤: 1. **生成客户端代理**:使用`wsimport`工具,根据服务的WSDL生成Java客户端代理类。 ```bash wsimport -keep ...

    如何基于JAX-WS开发一个WebService实例

    JAX-WS(Java API for XML Web Services)是Java平台上的一个标准,用于创建和消费Web服务。本篇将深入讲解如何基于JAX-WS开发一个WebService实例。 首先,让我们了解JAX-WS的基本概念。JAX-WS提供了一种简单的方式...

    JAX-WS2.1用户指南

    JAX-WS遵循WS-I(Web Services Interoperability)规范,确保与不同平台和语言实现的Web服务之间的互操作性。 通过学习和实践JAX-WS 2.1用户指南,开发者将能够熟练掌握Web服务的开发、部署和消费,提升其在分布式...

    JAX-WS规范

    Java API for XML Web Services(JAX-WS)是Java平台上的一个标准,用于创建Web服务和客户端。它提供了一种简单、类型安全的方式来构建和消费基于SOAP的消息传递应用程序,是Java世界中实现Web服务的核心框架之一。...

    Jax-ws RI.zip

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

    metro-jax-ws-master

    The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services, particularly SOAP services. JAX-WS is one of the Java XML programming APIs. It's a part of the ...

    webservice Demo注解+jax-ws

    在这个“webservice Demo注解+jax-ws”示例中,我们将深入探讨Web服务的注解使用以及基于Java API for XML Web Services (JAX-WS)的实现。 首先,让我们了解一下JAX-WS。它是Java平台的标准组件,用于创建和处理Web...

    metro-jax-ws-jaxws221x.zip

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

    jax-ws webservice简单demo

    Java API for XML Web Services(JAX-WS)是Java平台上的一个标准,用于构建和部署Web服务。它简化了Web服务的开发,使得开发者能够使用Java编程语言来创建、调用和部署SOAP(Simple Object Access Protocol)服务。...

Global site tag (gtag.js) - Google Analytics