axis部署WebServices 和 JAXB的运用 (一)

准备:
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
内容如下:
<? xmlversion="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;
public class SayHello {
publicStringsayHello(Stringname){
return"Hello"+name+",Welcome!";
}

}

sayHello添加到server-config.wsdd中:

如下所示:

<? xmlversion="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 >
<!-- compileJavasourcefiles -->
< target name ="compile" >
<!-- generatetheJavacontentclassesfromtheschema -->
< echo message ="Compilingtheschemaexternalbindingfile..." />
< xjc schema ="po.xsd" package ="primer.po" target ="src" />
<!-- compileallofthejavasources -->
< echo message ="Compilingthejavasourcefiles..." />
</ 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;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import javax.xml.bind. * ;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import primer.po. * ;
public class Main{
publicstaticvoidmain(String[]args){
try{
JAXBContextjc
=JAXBContext.newInstance("primer.po");
PurchaseOrderTypepo
=newPurchaseOrderType();
po.setOrderDate(getDate());
USAddressshipTo
=createUSAddress("AliceSmith",
"123MapleStreet",
"Cambridge",
"MA",
"12345");
po.setShipTo(shipTo);
USAddressbillTo
=createUSAddress("RobertSmith",
"8OakAvenue",
"Cambridge",
"MA",
"12345");
po.setBillTo(billTo);
Itemsitems
=newItems();
List
<Items.Item>itemList=items.getItem();

//startaddingItemTypeobjectsintoit
itemList.add(createItem("Nosferatu-SpecialEdition(1929)",
newBigInteger("5"),
newBigDecimal("19.99"),
null,
null,
"242-NO"));
itemList.add(createItem(
"TheMummy(1959)",
newBigInteger("3"),
newBigDecimal("19.98"),
null,
null,
"242-MU"));
itemList.add(createItem(
"GodzillaandMothra:BattleforEarth/Godzillavs.KingGhidora",
newBigInteger("3"),
newBigDecimal("27.95"),
null,
null,
"242-GZ"));
po.setItems(items);
JAXBElement
<PurchaseOrderType>poElement=(newObjectFactory()).createPurchaseOrder(po);
Marshallerm
=jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);

m.marshal(poElement,
newFileOutputStream("test.xml"));

m.marshal(poElement,System.out);

}
catch(JAXBExceptionje){
je.printStackTrace();
}
catch(IOExceptionioe){
ioe.printStackTrace();
}

}

publicstaticUSAddresscreateUSAddress(Stringname,Stringstreet,
Stringcity,Stringstate,
Stringzip)
{
USAddressaddress
=newUSAddress();
address.setName(name);
address.setStreet(street);
address.setCity(city);
address.setState(state);
address.setZip(
newBigDecimal(zip));
returnaddress;
}

publicstaticItems.ItemcreateItem(StringproductName,
BigIntegerquantity,
BigDecimalprice,
Stringcomment,
XMLGregorianCalendarshipDate,
StringpartNum)
{
Items.Itemitem
=newItems.Item();
item.setProductName(productName);
item.setQuantity(quantity);
item.setUSPrice(price);
item.setComment(comment);
item.setShipDate(shipDate);
item.setPartNum(partNum);
returnitem;
}

privatestaticXMLGregorianCalendargetDate(){
try{
returnDatatypeFactory.newInstance().newXMLGregorianCalendar(newGregorianCalendar());
}
catch(DatatypeConfigurationExceptione){
thrownewError(e);
}

}

}

Java Application运行 SayHelloClient.java

输出结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值