开发环境:
JDK:jdk1.5.0_16
服务器:jboss-4.2.2.GA
JWS面向服务的开发有三中方式,从JAVA到WSDL方式是最简单的,利用JAX-WS的注解,在JAVA类上进行标识,然后以Servlet的形式发布,服务器在解析的过程中自动生成WSDL。
从WSDL到JAVA也比较简单,只是不太常用,不过还是有必要了解到底是怎么回事。
JBOSS自带了一个工具来实现此功能,ANT任务使用的类为:org.jboss.wsf.spi.tools.ant.WSConsumeTask,它为调用wsconsume命令
先看build.xml
<?xml version="1.0"?>
<project name="wsdltojava" default="gen_java" basedir=".">
<property name="dir.src" value="src"/>
<property name="jboss.server.config" value="default"/>
<property name="jboss.net.version" value="4.0"/>
<property name="jboss.home" value="D:/jboss-4.2.2.GA"/>
<!-- 由于本人机子上有几个JDK,所以另起了一个名字 -->
<property name="java5.home" value="C:/jdk1.5.0_16"/>
<path id="web.service.classpath">
<fileset dir="${java5.home}/lib/" includes="*.jar" />
<fileset dir="${jboss.home}/lib/" includes="*.jar" />
<fileset dir="${jboss.home}/lib/endorsed/" includes="*.jar" />
<!-- Please dont put jaxws-tools.jar and jaxws-rt.jar on
Eclipse's classpath at all, its fine within ant. Its
to do with JAXB API bundled in Eclispe and Jboss -->
<fileset dir="${jboss.home}/client/">
<include name="activation.jar" />
<include name="getopt.jar" />
<include name="wstx.jar" />
<include name="jbossall-client.jar" />
<include name="log4j.jar" />
<include name="mail.jar" />
<include name="jbossws-spi.jar" />
<include name="stax-api.jar" />
<include name="jaxb-api.jar" />
<include name="jaxb-impl.jar" />
<include name="jaxb-xjc.jar" />
<include name="streambuffer.jar" />
<include name="stax-ex.jar" />
<include name="javassist.jar" />
<include name="jboss-xml-binding.jar" />
<include name="jbossws-client.jar" />
<include name="jboss-jaxws.jar" />
<include name="jboss-jaxrpc.jar" />
<include name="jboss-saaj.jar" />
<include name="jboss-srp-client.jar" />
<include name="jbossws-common.jar" />
<include name="jbossws-core.jar" />
<!-- Be careful with these two jars in Eclipse,
dont put it on classpath -->
<include name="jaxws-tools.jar" />
<include name="jaxws-rt.jar" />
</fileset>
</path>
<target name="gen_java">
<taskdef name="wsconsume" classname="org.jboss.wsf.spi.tools.ant.WSConsumeTask">
<classpath>
<path refid="web.service.classpath" />
</classpath>
</taskdef>
<wsconsume
fork="true"
verbose="true"
sourcedestdir="${dir.src}"
package="com.client"
keep="true"
wsdl="wsdl/ticket-impl.wsdl" />
</target>
</project>
下面给出两个wsdl文件
ticket-impl.wsdl
<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="http://jbpm.org/examples/ticket"
xmlns:tns="http://jbpm.org/examples/ticket" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<import namespace="http://jbpm.org/examples/ticket" location="ticket.wsdl" />
<binding name="TicketIssuerBinding" type="tns:TicketIssuer">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="createTicket">
<soap:operation soapAction="http://jbpm.org/examples/ticket#createTicket" />
<input>
<soap:body use="literal" namespace="http://jbpm.org/examples/ticket" />
</input>
<output>
<soap:body use="literal" namespace="http://jbpm.org/examples/ticket" />
</output>
</operation>
</binding>
<service name="TicketService">
<port name="TicketIssuerPort" binding="tns:TicketIssuerBinding">
<soap:address location="REPLACE_WITH_ACTUAL_URI" />
</port>
</service>
</definitions>
ticket.wsdl
<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="http://jbpm.org/examples/ticket"
xmlns:tns="http://jbpm.org/examples/ticket" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="ticketRequest">
<documentation>ticket creation request</documentation>
</message>
<message name="ticketMessage">
<documentation>ticket number wrapper</documentation>
<part name="ticketNo" type="xsd:int" />
</message>
<portType name="TicketIssuer">
<documentation>interface to ticket issuer service</documentation>
<operation name="createTicket">
<documentation>generate a ticket number, distinct from previous calls</documentation>
<input message="tns:ticketRequest" />
<output message="tns:ticketMessage" />
</operation>
</portType>
</definitions>
用ANT执行一下,就会在src/com/client 下生成TicketService.java和TicketIssuer.java两个JAVA文件
虽然看起来有点麻烦,但理解起来其实也挺简单的。不过还有一种从WSDL和JAVA的开发模式就比较困难一点了,有机会也会去试一下。