服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用java web services developer pack1.6开发webservice入门

介绍

本文主要介绍java web services developers pack1.6的基础知识,并且利用eclipse开发一个入门程序。

java web services developer pack简介

java web services developer pack(jwsdp)是一个免费的集成工具,它帮助java开发者使用最新的web services技术和标准实现建立和测试xml程序、web services和web应用程序。jwsdp当前最新版本是2.0,而本文的讲解和示例程序是基于1.6的。更多详细信息请参考http://java.sun.com/webservices/jwsdp/index.jsp

jwsdp1.6包含了下列技术:

l     xml and web services security v2.0 ea

l     xml digital signatures v1.0 fcs

l     java architecture for xml binding (jaxb) v1.0.5 fcs

l     java api for xml processing (jaxp) v1.3.1 fcs

l     java api for xml registries (jaxr) v1.0.8 fcs

l     java api for xml-based rpc (jax-rpc) v1.1.3 fcs

l     soap with attachments api for java (saaj) v1.2.2 fcs

l     javaserver pages standard tag library (jstl) v1.1.1_01 fcs

l     sun service registry v3.0 ea

l     ant build tool 1.6.2 fcs

l     ws-i attachments sample application 1.0 ea5

wscompile和wsdeploy工具介绍

这两个工具是由jax-rpc标准实现提供的,和jwsdp包一起发布。开发者通过使用它们可以开发标准的web services以及客户端实现。

1、 wscompile

wscompile主要用来生成在jax-rpc客户端和服务中使用的stubs、ties、serializers和wsdl文件。这个工具使用一个配置文件作为输入,配置文件一般指定一个wsdl文件、一个模型文件或者一个经过的编译的服务终端接口。

wscompile可以在命令中使用,也可以通过ant脚本调用,命令行中的语法是:

wscompile [options] <configuration-file>

为了方便起见,一般把配置文件命名为config.xml,当然这并不是必须,你也可以命名为你喜欢的任何名字。关于options的详细信息请参考jaxrpc-tools文档,这里就不再赘述了。我们主要看一下配置文件的格式。

配置文件包含了描述web service的信息,基本格式如下:

<?xml version="1.0" encoding="utf-8"?>

<configuration

    xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">

        <service> or <wsdl> or <modelfile>

</configuration>

其中<configuration>元素必须包含<service>、<wsdl>或者<modelfile>元素中的一个。

<service>元素

如果指定了这个元素,wscompile就会读取描述服务的终端接口,并且生成一个wsdl文件。在<interface>子元素中,name属性指定了服务终端接口,servantname属性指定实现接口的类。例如:

service name="collectionif_service"  

targetnamespace="http://echoservice.org/wsdl"  

      typenamespace="http://echoservice.org/types"  

      packagename="stub_tie_generator_test">  

    <interface name="stub_tie_generator_test.collectionif"  

          servantname="stub_tie_generator_test.collectionimpl"/>  

</service>

<wsdl>元素

如果指定了这个元素,wscompile会读取服务的wsdl文件,产生服务终端接口。location属性指定了wsdl文件的url,而packagename属性指定了由wscompile产生的类所在的包。例如:

<wsdl

        location="./etc/helloworldservice.wsdl"

        packagename="hello">

    </wsdl>

<modelfile>元素

这个元素主要为高级用户而设置。

如果config.xml文件包含了一个<service>或者<wsdl>元素,wscomplile就会生成一个model文件,它包含了描述服务的内在数据结构。如果你已经手动生成了一个model文件,那么在下一次运行wscompile时可以重用。例如:

<modelfile location="mymodel.xml.gz"/>  

2、 wsdeploy

wsdeploy工具读取一个war文件和jaxrpc-ri.xml文件,然后产生另外一个可以用来发布的war文件。在这个操作背后,wsdeploy运行了包含-gen:server选项的wscompile。wscompile命令产生类和一个wsdl文件,这些要被包含在wsdeploy产生的war文件中。

和wscompile一样,wsdeploy也可以通过命令行和ant脚本两种方式调用,下面是命令行的语法:

wsdeploy -o <output-war-file> <input-war-file> <options>

<options>具体意义参考jaxrpc-tools文档。

输入的war文件

你可以通过任何方式(ide、ant等)产生输入的war文件,这个文件典型结构如下:

meta-inf/manifest.mf

web-inf/classes/hello/helloif.class

web-inf/classes/hello/helloimpl.class

web-inf/jaxrpc-ri.xml

web-inf/web.xml

这上面的例子中helloif是服务终端接口,而helloimpl是实现这个接口的类。web.xml文件是web组件的描述符文件。接下来介绍jaxrpc-ri.xml文件。

jaxrpc-ri.xml文件

下面的清单是关于一个简单的helloworld服务的jaxrpc-ri.xml文件:

<?xml version="1.0" encoding="utf-8"?>  

<webservices  

       xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd"  

       version="1.0"  

       targetnamespacebase="http://com.test/wsdl"  

       typenamespacebase="http://com.test/types"  

       urlpatternbase="/ws">  

       <endpoint  

           name="myhello"  

           displayname="helloworld service"  

           description="a simple web service"  

           wsdl="/web-inf/<wsdlame>  

           interface="hello.helloif"  

           implementation="hello.helloimpl"/>  

       <endpointmapping  

           endpointname="myhello"  

           urlpattern="/hello"/>  

</webservices>  

<webservices>元素必须一个或者多个<endpoint>元素。在这个例子中,注意<endpoint>元素的interface和implementation属性指定了服务的接口和实现类。<endpointmapping>元素将服务名和url联系起来。

使用jwsdp实现一个简单的helloservice

1、  开发环境:jwsdp1.6、tomcat50-jwsdp,开发工具eclipse3.1(已配置好)。

2、  下载所需工具:java web services developers pack1.6和tomcat50-jwsdp。下载地址:http://java.sun.com/webservices/jwsdp/index.jsp

3、  安装

(1)       tomcat50-jwsdp,下载后直接解压到指定目录即可,本文是e:/tomcat50-jwsdp。

(2)       jwsdp1.6的安装:下载的应该是一个可执行文件jwsdp-1_6-windows-i586.exe,双击即开始安装。注意:jwsdp1.6需要jdk1.4或者以上版本;在安装过程中会让指定web服务器,这里就指定为刚才安装的tomcat。当然也可以不指定,需要时再根据文档手动指定。

4、  开发过程

一切准备工作都已就绪,下面就开始具体的实现吧。

(1)       新建工程:打开eclipse,选择new―〉project,在web中选择dynamic web project。“next”,在project name中输入工程名:helloworld。target runtime指定了运行时的服务器,如果列表中没有需要的服务器,可以点击“new”重新指定。
使用java web services developer pack1.6开发webservice入门(图一)

点击查看大图

图1 输入工程名、指定服务器

一直下一步,完成工程新建。

(2)       添加类库:在工程名单击右键,选择“properties”―〉“java build path”。“add libraries”,在“add library”窗口中选择“user library”,“next”,如果“selected user library”中不存在需要的类库,点击“user libraries”添加。所需要的jar文件列表如图2中所示:
使用java web services developer pack1.6开发webservice入门(图二)

图2  程序需要的jar列表

(3)       新建接口类和实现类:建立接口和类的方法非常简单,没有必要在叙述,两个文件分别为:helloif.java和helloimpl.java下面是两个文件的代码列表:

//helloif.java

package hello;

 

import java.rmi.remote;

import java.rmi.remoteexception;

 

public interface helloif extends remote{

 

       public string sayhello(string str) throws remoteexception;

}

 

//helloimpl.java

package hello;

 

public class helloimpl implements helloif {

      

       //

       private string hello = "sayhello";

 

       public string sayhello(string str) {

              return str + hello;

       }

}

呵呵,是不是太简单了?不过这已经足够了。

(4)       建立jaxrpc-ri.xml文件:因为在例子中只用了wsdeploy工具,所以我们需要一个jaxrpc-ri.xml文件。在“webcontent”下的“web-inf”节点上单击右键,选择“other”―〉选择“xml”下的“xml”,输入文件名“jaxrpc-ri.xml”。jaxrpc-ri.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?>

<webservices

    xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd"

    version="1.0"

    targetnamespacebase="http://hello.org/wsdl"

    typenamespacebase="http://hello.org/types"

    urlpatternbase="/webservices">

 

    <endpoint

        name="hello"

        displayname="helloservice"

        description="an example sso token service"

        interface="hello.helloif" 

        implementation="hello.helloimpl"/>

 

    <endpointmapping

        endpointname="hello"

        urlpattern="/hello"/>

 

</webservices>

(5)       建立ant脚本build.xml文件:过程和上面一样,文件位置在工程根目录下,名字为build.xml,内容如下:

<?xml version="1.0" encoding="utf-8"?>

<project name="helloworld" default="all" basedir=".">

 

  <property name="jwsdp_home"  value="e:/jwsdp-1.6"/>

  <property name="tmpdir"      value="./tmp"/>

  <property name="webclasses"  value="./webcontent/web-inf/classes"/>

    <property name="tomcat_home" value="e:/tomcat50-jwsdp"/>

 

  <target name="clean">

    <echo message="删除目录"/>

 

    <delete file="./${ant.project.name}.war" />

    <delete file="./hello-temp.war" />

    <delete dir="${tmpdir}"/>

    <delete dir="${webclasses}"/>

  </target>

 

  <target name="init" depends="clean">

    <mkdir dir="${webclasses}"/>

    <mkdir dir="${tmpdir}"/>

  </target>

 

  <target name="create-war" depends="init">

   <copy todir="./webcontent/web-inf/classes">

     <fileset dir="./build/classes"/>

   </copy>

   <jar jarfile="./hello-temp.war" basedir="./webcontent"/>

  </target>

 

  <target name="deployready" depends="create-war">

    <exec executable="${jwsdp_home}/jaxrpc/bin/wsdeploy.bat">

      <arg line="-tmpdir tmp/ -o ${ant.project.name}.war hello-temp.war"/>

    </exec>

  </target>

   

    <target name="deploy" depends="deployready">

       <delete file="${tomcat_home}/webapps/${ant.project.name}.war"/>

       <copy file="./${ant.project.name}.war" tofile="${tomcat_home}/webapps/${ant.project.name}.war"/>

    </target>

   

    <target name="run-server">

       <exec executable="${tomcat_home}/bin/startup.bat"/>

    </target>

 

  <target name="all" depends="deploy">

  </target>

</project>

经过以上5个步骤已经建立了程序需要的所有文件,剩下的工作就是测试了。工程中文件结构如下图:
使用java web services developer pack1.6开发webservice入门(图三)

图3  例子的文件结构

(6)       测试和运行:将build.xml文件添加到eclipse的ant窗口中,如图4。
使用java web services developer pack1.6开发webservice入门(图四)

图4  ant窗口

双击all任务,可以在console窗口中看到如下信息:
使用java web services developer pack1.6开发webservice入门(图五)

点击查看大图

图5 控制台输出信息

证明程序编译发布成功。我们可以通过winrar等工具查看由wsdeploy工具生成的helloworld.war文件的结构:

web-inf下:
使用java web services developer pack1.6开发webservice入门(图六)

图6

web-inf/classes/hello下:
使用java web services developer pack1.6开发webservice入门(图七)

图7

web-inf/classes/jaxrpc/generated/hello下:
使用java web services developer pack1.6开发webservice入门(图八)

图8

在ant窗口中双击“run-server”任务启动服务器,打开浏览器,在其中输入http://localhost:8080/hellowolrd/hello?wsdl,可以看到wsdl文件内容。证明程序成功运行。

注意:本文没有实现webservice的客户端,那么就留作练习吧。

总结

   本文简单的介绍了java web services developer pack的基本概念,以及wscompile和wsdeploy使用方法,并且结合jwsdp1.6实现了一个简单的helloworld程序

扫描关注微信公众号