axis部署WebServices 和 JAXB的运用 (一)
发布时间:2020-12-17 02:43:32 所属栏目:安全 来源:网络整理
导读:准备: Axis 是 Apache 的一个开源 webservice 引擎。它目前最为成熟的开源 webservice 引擎之一。 部署环境:MyEclipse 5.5.1 GA + JDK1.5 + JAXB+Axis1.3 Axis 下载地址: http://www.uniontransit.com/apache/ws/axis/1_3/axis-bin-1_3.zip (apache) JX
准备:
Axis
是
Apache
的一个开源
webservice
引擎。它目前最为成熟的开源
webservice
引擎之一。
部署环境:MyEclipse 5.5.1 GA + JDK1.5 + JAXB+Axis1.3
Axis 下载地址:
http://www.uniontransit.com/apache/ws/axis/1_3/axis-bin-1_3.zip (apache)
JXAB下载地址:
http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jwsdp-2_0-windows-i586.exe?BundledLineItemUUID=Jw1IBe.mXVgAAAEZejo1uDh.&OrderID=4rNIBe.mPoMAAAEZajo1uDh.&ProductID=BS3ACUFBcEgAAAEY2f45AXub&FileName=/jwsdp-2_0-windows-i586.exe
(sun)
其它所需的包:activation.jar 和 mail.jar (都可以在SUN官网下载)
地址:
http://www.cppblog.com/Files/wform/mail-activation.rar
(sun)
(以上地址都可以直接下载,如有变动,可以分别到apache官网和sun官网下载,其中jaxb是在jwsdp安装后的子目录中)
一、部署工程:
1
、新建一个Web工程,名称axisweb
2
、解压下载的axis压缩包,
将
AXIS_UNZIP_PATHaxis-versionwebapps
下的
axis
中的所有拷贝到新建工程
axisweb
的
WebRoot
下,可以覆盖掉
WEB-INF
。
3
、
将
jwsdp
安装目录下
jaxblib
中的包拷贝到
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", "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, "242-NO")); itemList.add(createItem("TheMummy(1959)", newBigInteger("3"), newBigDecimal("19.98"), "242-MU")); itemList.add(createItem("GodzillaandMothra:BattleforEarth/Godzillavs.KingGhidora", newBigDecimal("27.95"), "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 输出结果:
http://p.blog.csdn.net/images/p_blog_csdn_net/isle_ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |