微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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",

"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_

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐