准备:
Axis
是
Apache
的一个开源
webservice
引擎。它目前最为成熟的开源
webservice
引擎之一。
部署环境:MyEclipse 5.5.1 GA + JDK1.5 + JAXB+Axis1.3
JXAB下载地址:
其它所需的包:activation.jar 和 mail.jar (都可以在SUN官网下载)
(以上地址都可以直接下载,如有变动,可以分别到apache官网和sun官网下载,其中jaxb是在jwsdp安装后的子目录中)
一、部署工程:
1
、新建一个Web工程,名称axisweb
2
、解压下载的axis压缩包,
将
AXIS_UNZIP_PATH/axis-version/webapps
下的
axis
中的所有拷贝到新建工程
axisweb
的
WebRoot
下,可以覆盖掉
WEB-INF
。
3
、
将
jwsdp
安装目录下
/jaxb/lib
中的包拷贝到
axisweb
工程
的
lib
中,及下载的
active.jar
和
mail.jar
也放到
lib
中去。
4
、
在
axisweb
工程的
WEB-INF
下新建一个
server-config.wsdd
内容如下:
<?
xml version="1.0" encoding="UTF-8"
?>
<
deployment
xmlns
="http://xml.apache.org/axis/wsdd/"
xmlns:java
="http://xml.apache.org/axis/wsdd/providers/java"
>
<
globalConfiguration
>
<
parameter
name
="adminPassword"
value
="admin"
/>
<
parameter
name
="attachments.Directory"
value
="./attachments"
/>
<
parameter
name
="attachments.implementation"
value
="org.apache.axis.attachments.AttachmentsImpl"
/>
<
parameter
name
="sendXsiTypes"
value
="true"
/>
<
parameter
name
="sendMultiRefs"
value
="true"
/>
<
parameter
name
="sendXMLDeclaration"
value
="true"
/>
<
parameter
name
="axis.sendMinimizedElements"
value
="true"
/>
<
requestFlow
>
<
handler
type
="java:org.apache.axis.handlers.JWSHandler"
>
<
parameter
name
="scope"
value
="session"
/>
</
handler
>
<
handler
type
="java:org.apache.axis.handlers.JWSHandler"
>
<
parameter
name
="scope"
value
="request"
/>
<
parameter
name
="extension"
value
=".jwr"
/>
</
handler
>
</
requestFlow
>
</
globalConfiguration
>
<
handler
name
="LocalResponder"
type
="java:org.apache.axis.transport.local.LocalResponder"
/>
<
handler
name
="URLMapper"
type
="java:org.apache.axis.handlers.http.URLMapper"
/>
<
handler
name
="Authenticate"
type
="java:org.apache.axis.handlers.SimpleAuthenticationHandler"
/>
<
service
name
="AdminService"
provider
="java:MSG"
>
<
parameter
name
="allowedMethods"
value
="AdminService"
/>
<
parameter
name
="enableRemoteAdmin"
value
="false"
/>
<
parameter
name
="className"
value
="org.apache.axis.utils.Admin"
/>
<
namespace
>
http://xml.apache.org/axis/wsdd/
</
namespace
>
</
service
>
<
service
name
="Version"
provider
="java:RPC"
>
<
parameter
name
="allowedMethods"
value
="getVersion"
/>
<
parameter
name
="className"
value
="org.apache.axis.Version"
/>
</
service
>
<
transport
name
="http"
>
<
requestFlow
>
<
handler
type
="URLMapper"
/>
<
handler
type
="java:org.apache.axis.handlers.http.HTTPAuthHandler"
/>
</
requestFlow
>
</
transport
>
<
transport
name
="local"
>
<
responseFlow
>
<
handler
type
="LocalResponder"
/>
</
responseFlow
>
</
transport
>
</
deployment
>
注:也可以下载:http://www.cppblog.com/Files/wform/server-config.rar <service></service>中用于添加暴露给客户端的服务(即方法),后面将会给出示例。
二、建立web服务及服务验证
1、编写服务器端程序SayHello.java
新建一个com包,新建一个SayHello.java:
package
com;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
SayHello
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String sayHello(String name){
return "Hello " + name + ",Welcome!";
}
}
将sayHello添加到server-config.wsdd中:
如下所示:
<?
xml version="1.0"
?>
<
project
basedir
="."
default
="compile"
>
<!--
这里是jwsdp的安装目录
-->
<
property
name
="jwsdp.home"
value
="C:jwsdp-2.0"
/>
<
path
id
="classpath"
>
<
pathelement
path
="build"
/>
<
fileset
dir
="${jwsdp.home}"
includes
="jaxb/lib/*.jar"
/>
<
fileset
dir
="${jwsdp.home}"
includes
="jwsdp-shared/lib/*.jar"
/>
<
fileset
dir
="${jwsdp.home}"
includes
="jaxp/lib/**/*.jar"
/>
</
path
>
<
taskdef
name
="xjc"
classname
="com.sun.tools.xjc.XJCTask"
>
<
classpath
refid
="classpath"
/>
</
taskdef
>
<!--
compile Java source files
-->
<
target
name
="compile"
>
<!--
generate the Java content classes from the schema
-->
<
echo
message
="Compiling the schema external binding file..."
/>
<
xjc
schema
="po.xsd"
package
="primer.po"
target
="src"
/>
<!--
compile all of the java sources
-->
<
echo
message
="Compiling the java source files..."
/>
</
target
>
</
project
>
Run As -> Ant Build XML Schema就可以生成对应的实体类,当然po.sxd 和 build.xml都应位于工程的根目录下!
3、写一个测试的java文件
Main.java:
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.math.BigDecimal;
import
java.math.BigInteger;
import
java.util.GregorianCalendar;
import
java.util.List;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.xml.bind.JAXBContext;
import
javax.xml.bind.JAXBElement;
import
javax.xml.bind.JAXBException;
import
javax.xml.bind.Marshaller;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.xml.bind.
*
;
import
javax.xml.datatype.DatatypeFactory;
import
javax.xml.datatype.XMLGregorianCalendar;
import
javax.xml.datatype.DatatypeConfigurationException;
import
primer.po.
*
;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Main {
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main( String[] args ) {
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try {
JAXBContext jc = JAXBContext.newInstance( "primer.po" );
PurchaseOrderType po = new PurchaseOrderType();
po.setOrderDate( getDate() );
USAddress shipTo = createUSAddress( "Alice Smith",
"123 Maple Street",
"Cambridge",
"MA",
"12345" );
po.setShipTo( shipTo );
USAddress billTo = createUSAddress( "Robert Smith",
"8 Oak Avenue",
"Cambridge",
"MA",
"12345" );
po.setBillTo( billTo );
Items items = new Items();
List<Items.Item> itemList = items.getItem();
// start adding ItemType objects into it
itemList.add( createItem( "Nosferatu - Special Edition (1929)",
new BigInteger( "5" ),
new BigDecimal( "19.99" ),
null,
null,
"242-NO" ) );
itemList.add( createItem( "The Mummy (1959)",
new BigInteger( "3" ),
new BigDecimal( "19.98" ),
null,
null,
"242-MU" ) );
itemList.add( createItem( "Godzilla and Mothra: Battle for Earth/Godzilla vs. King Ghidora",
new BigInteger( "3" ),
new BigDecimal( "27.95" ),
null,
null,
"242-GZ" ) );
po.setItems( items );
JAXBElement<PurchaseOrderType> poElement = (new ObjectFactory()).createPurchaseOrder(po);
Marshaller m = jc.createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
m.marshal(poElement, new FileOutputStream("test.xml"));
m.marshal(poElement, System.out);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch( JAXBException je ) {
je.printStackTrace();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public static USAddress createUSAddress( String name, String street,
String city, String state,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
String zip ){
USAddress address = new USAddress();
address.setName( name );
address.setStreet( street );
address.setCity( city );
address.setState( state );
address.setZip( new BigDecimal( zip ) );
return address;
}
public static Items.Item createItem( String productName,
BigInteger quantity,
BigDecimal price,
String comment,
XMLGregorianCalendar shipDate,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
String partNum ) {
Items.Item item = new Items.Item();
item.setProductName( productName );
item.setQuantity( quantity );
item.setUSPrice( price );
item.setComment( comment );
item.setShipDate( shipDate );
item.setPartNum( partNum );
return item;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static XMLGregorianCalendar getDate() {
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (DatatypeConfigurationException e) {
throw new Error(e);
}
}
}
用Java Application运行 SayHelloClient.java
输出结果:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/isle_t/clip_image007.jpg)
在axisweb工程根目录下生成了一个test.xml 的文件,内容和上面一样!
<
service
name
="sayHello"
provider
="java:RPC"
>
<
parameter
name
="className"
value
="com.SayHello"
/>
<
parameter
name
="allowedMethods"
value
="sayHello"
/>
*代表所有方法
</
service
>
注:
server-config.wsdd
中不只一个
<service>,
如果要其它方法,再写一个
<service>
节点即可!
2、验证服务是否可行:
在浏览器中输入 http://localhost:端口号/axisweb/services
可以看到如下结果:
我们可以看到自动生成了wsdl文件,三个wsdl分别对应到了server-config.wsdd中的三个<service>节点,wsdl就是我们把要提供给客户端的。客户端通过wsdl文件,可以知道他可以用的服务。在这里sayHello 为客户端提供了一个返回一句”Hello ** ,welcome!”功能的服务。
3、编写客户端程序:SayHelloClient.java:
import
org.apache.axis.client.Call;
import
org.apache.axis.client.Service;
import
javax.xml.rpc.ParameterMode;
import
org.apache.axis.encoding.XMLType;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
SayHelloClient
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) {
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try {
String endpoint = "http://localhost:8080/axisweb/services/sayHello";
String name=" tongxiaodao";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.addParameter("param",XMLType.XSD_STRING,ParameterMode.IN);
call.setOperationName( "sayHello" );
call.setReturnType( XMLType.XSD_STRING );
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
String ret = (String) call.invoke( new Object[]{ name } );
System.out.println(ret);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
(1)用Java Application运行 SayHelloClient.java
得到结果如下:
(2)可以用wsCaller 调用服务端的服务。
wsCaller下载地址:http://www.tiantiansoft.com/files_down/java/200551084214548.rar
调用过程好下:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/isle_t/clip_image004.jpg)
可以看到要输入一下参数name ,在这我们输入 nannan,点击invoke 进行调用,返回如下:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/isle_t/clip_image005.jpg)