这几天也一直在为了设计与实现一个业务平台,想用Webservice 来作为核心技术来实现,也一直想精溢求精地把这个东东搞好, 突然觉得好久没来这里写日志了.呵,时间如白驹过隙啊!
对于webService 以前也接触过.但只是皮毛,这次可是来真格的.来就来吧,年轻人这就是机会.一定得把握好,干它一票! 呵,就在网上到处找找啊,在本地机器上左试试,右试试啊, 什么法子都用一遍。就不信搞不定它,小样!
网上的例子也比较散,比较简易的,达不我想要的业务水平,只能慢慢搜索与摸索吧。。。。。。就这样天天想着这事,也没什么心情来这里写写。
近些天,也快回深圳了,定在下周四动身吧。这边的工作也得告一节了。把我前一些日子,搞一个完整的Samples 放到这里来,与大家共享。希望对有心人有些启发,也希望高人指点与点评。。。。。。。、
****************************************************
*Axis开发Webservice传Bean对象返回String串的实例.rtf*
****************************************************
在ECLIPSE 里新建一工程,其次建包,再次建类,
基本包:samples.userguide.example5
基本类: Order.java
内容:
* Order.java *
*《********************************
package samples.userguide.example5;
/**
* This is a JavaBean which represents an order for some products.
* Copyright: Copyright (c) 2007-1
* @author Black skin (blackskin@126.com)
* @version 1.0
*/
public class Order {
/** Who's ordering */
private String customerName;
/** Where do they live */
private String shippingAddress;
/** Which items do we want */
private String itemCodes[];
/** And how many */
private int quantities[];
// Bean accessors
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String name) {
customerName = name;
}
public String getShippingAddress() {
return shippingAddress;
}
public void setShippingAddress(String address) {
shippingAddress = address;
}
public String[] getItemCodes() {
return itemCodes;
}
public void setItemCodes(String[] items) {
itemCodes = items;
}
public int[] getQuantities() {
return quantities;
}
public void setQuantities(int[] quants) {
quantities = quants;
}
}
********************************》*
主服务包: samples.userguide.example5
主服务类: BeanService.java
如下:
* BeanService.java *
**《*******************************
package samples.userguide.example5;
/**
* This is a JavaBean which represents an order for some products.
* Copyright: Copyright (c) 2007-1
* @author Black skin (blackskin@126.com)
* @version 1.0
*/
public class BeanService {
public String processOrder(Order order) {
String sep = System.getProperty("line.separator");
String response = "Hi, " + order.getCustomerName() + "!" + sep;
response += sep + "You seem to have ordered the following:" + sep;
String[] items = order.getItemCodes();
int[] quantities = order.getQuantities();
for (int i = 0; i < items.length; i++) {
response += sep + quantities[i] + " of item : " + items[i];
}
response += sep + sep + "If this had been a real order processing system, "
+ sep + "we'd probably have charged you about now.";
return response;
}
public static void main(String[] args) throws Exception{
/** Who's ordering */
String customerName = "Black Skin";
/** Where do they live */
String shippingAddress = "001 xingan Street, bayuquan, YK";
/** Which items do we want */
String[] items = new String[] { "apple", "160ml-Milk" };
/** And how many */
int[] quantities = new int[] { 2, 4 };
/** request class init() */
Order order = new Order();
order.setCustomerName(customerName);
order.setShippingAddress(shippingAddress);
order.setItemCodes(items);
order.setQuantities(quantities);
/** server class init() */
BeanService beanservice = new BeanService();
String result = beanservice.processOrder(order);
/** OutPut the result */
System.out.println(" " + result);
}
}
********************************》*
BeanService.java里有个本地测试方法(samples.userguide.example5.BeanService.main()),来测试此应用的可运行性;
到此,服务端类写完了,接着来把此类的方法发布成 WebService 接口
手写一个deploy.wsdd 文件,可以参考下面的例子:
*deploy.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">
<service name="服务名" provider="java:RPC">
<parameter name="className" value="服务类文件的实际路径(包括包的信息)"/>
<parameter name="allowedMethods" value="*(即为所有方法都可访问,如要指定多个可用空格分隔开)"/>
<parameter name="allowedRoles" value="user"/>
<parameter name="scope" value="Session"/>
<requestFlow>
<handler type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
</requestFlow>
</service>
</deployment>
********************************》*
显然,简单,造一个deploy.wsdd 信手捏来,如下:
*deploy.wsdd*
*《********************************
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="OrderProcessor" provider="java:RPC">
<parameter name="className" value="samples.userguide.example5.BeanService"/>
<parameter name="allowedMethods" value="processOrder"/>
<beanMapping qname="myNS:Order" xmlns:myNS="urn:BeanService" languageSpecificType="java:samples.userguide.example5.Order"/>
</service>
</deployment>
********************************》*
有了deploy.wsdd文件了,接着通过它来生成 server-config.wsdd.
OK! 用命令直接生成就行;在deploy.wsdd 的文件夹下新建一个 makeWsdd.bat.
其中的写法有下面的例子来参考:
* makeWsdd.bat 例子*
*《********************************
set AXISCLASSPATH= “应用发布的路径”
set AXIS_LIB= “AXIS框架的内部JAR包LIB路径”
SET CLASSPATH=.;%CLASSPATH%;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\axis.jar;%AXIS_LIB%\jaxrpc.jar;
%AXIS_LIB%\saaj.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar
java -Djava.ext.dirs=%AXISCLASSPATH% org.apache.axis.client.AdminClient
-S服务名\services\AdminService -p端口号(如果你的端口号不是默认的8080需要指定。
例如:8899)deploy.wsdd
********************************》*
如此,我写了一“makeWsdd.bat”文件。如下:
* makeWsdd.bat*
*《********************************
set AXISCLASSPATH=C:\Axis_webservice\Tomcat_4.1\webapps\axis
set AXIS_LIB=C:\axis-1_4\lib
SET CLASSPATH=.;%CLASSPATH%;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\axis.jar;%AXIS_LIB%\jaxrpc.jar;
%AXIS_LIB%\saaj.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar
java -Djava.ext.dirs=%AXISCLASSPATH% org.apache.axis.client.AdminClient
-SBeanServer\services\AdminService -p 8080 deploy.wsdd
********************************》*
保存好,接下来架设Webservice.
环境架设:
首先,安装JDK...(略)...本例用JDK版本号V1.4.
其次,安装Tomcat...(略)...本例用Tomcat版本号V4.1.
再次,AXIS...(略)...本例用AXIS版本号V1.4. 去AXIS官方网站:http://www.apache.org下载AXIS,解压后得,
|- axis-1_4
|- docs
|- ..(略)...
|- lib
|- axis.jar
|- axis-ant.jar
|- commons-discovery-0.2.jar
|- commons-logging-1.0.4.jar
|- jaxrpc.jar
|- log4j-1.2.8.jar
|- log4j.properties
|- saaj.jar
|- wsdlj-1.5.1.jar
|- samples
|- ..(略)...
|- webapps
|- axis
|- ..(略)...
|- xmls
|- ..(略)...
将解压后,C:\axis-1_4\webapps 目录下的 axis 目录 Copy 到 tomcat/webapps目录(C:\Axis_webservice\Tomcat_4_1\webapps)下,
架设AXIS:
首先,将deploy.wsdd文件Copy到 %AXISCLASSPATH%/WEB-INF 目录下;
其次,将之前的类编译后,连路径一起Copy到 %AXISCLASSPATH%/WEB-INF/classes目录下,
最后,将tomcat的 catalina 服务启动.
(即:C:\Axis_webservice\Tomcat_4.1\bin,
[特别注意的是:]
A。tomcat务必启动。
B。在写如上的命令行时,务必注意参数的写法。“-D” 与 “-S”与其值间不能有空格。切记!
C。各XX的路径里,最好是不要有空格,因为JAVA里对大小写与路径空格有校验。
,)
测试:
命令行启动方法;运行"cmd",在命令行里写:"cd C:\Axis_webservice\Tomcat_4.1\bin",回车,再写:" catalina run ",回车,OK!
后台会报错:
"
- Unable to find config file. Creating new servlet engine config file: /WEB-INF/server-config.wsdd
2007-1-20 10:07:48 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on port 8080
"
说明,在../WEB-INF/下没有"server-config.wsdd",暂时忽略。
在IE地址栏写:http://localhost:8080/axis
可见,axis 欢迎首页,点击 "list" 便可看到默认情况下发布了两个服务下各有一个方法。一切准备工作已经OK!
再回到,刚才我们写的那个批处理文件:" makeWsdd.bat",
直接双击,便可执行, 有一个警告,可以忽略的。执行完成后,在相应的
%AXISCLASSPATH%/WEB-INF下便有一“server-config.wsdd”文件,如下:
*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="sendMultiRefs" value="true"/>
<parameter name="disablePrettyXML" value="true"/>
<parameter name="adminPassword" value="admin"/>
<parameter name="attachments.Directory" value="C:\Axis_webservice\Tomcat_4.1\webapps\axis\WEB-INF\attachments"/>
<parameter name="dotNetSoapEncFix" value="true"/>
<parameter name="enableNamespacePrefixOptimization" value="false"/>
<parameter name="sendXMLDeclaration" value="true"/>
<parameter name="sendXsiTypes" value="true"/>
<parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
<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>
<service name="OrderProcessor" provider="java:RPC">
<parameter name="allowedMethods" value="processOrder"/>
<parameter name="className" value="samples.userguide.example5.BeanService"/>
<beanMapping languageSpecificType="java:samples.userguide.example5.Order" qname="ns1:Order" xmlns:ns1="urn:BeanService"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
<handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
</requestFlow>
<parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
<parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
<parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
<parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
<parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
<parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
</transport>
<transport name="local">
<responseFlow>
<handler type="LocalResponder"/>
</responseFlow>
</transport>
</deployment>
********************************》*
再去打开:"http://localhost:8080/axis/servlet/AxisServlet",或者,再刷新。
便可以看到,我们新发布的一个webservice接口服务,其名为:"OrderProcessor "
此服务下的个方法:其名为:"processOrder "。
点击服务对应的"(WSDL)"可见服务WSDL描述内容。在其中也可以得服务的访问地址:
"http://localhost:8080/axis/services/OrderProcessor?wsdl"
服务器端的开发,搞定了!
-:) -:) -:)
接下来,准备开发,客户端的开发。
* Client.java *
*《********************************
package samples.userguide.example5;
/**
* This is a JavaBean which represents an order for some products.
* Copyright: Copyright (c) 2007-1
* @author Black skin (blackskin@126.com)
* @version 1.0
*/
import java.net.URL;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.utils.Options;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
public class Client {
public static void main(String[] args) throws Exception {
//Options options = new Options(args);
String urlWsdl = "http://localhost:8080/axis/services/OrderProcessor?wsdl";
Order order = new Order();
order.setCustomerName("Black Skin");
order.setShippingAddress("275 Grove Street, Newton, MA");
String[] items = new String[] { "apple", "160ml-Milk" };
int[] quantities = new int[] { 8, 4 };
order.setItemCodes(items);
order.setQuantities(quantities);
Service service = new Service();
Call call = (Call) service.createCall();
QName qn = new QName("urn:BeanService", "Order");
call.registerTypeMapping(Order.class, qn,
new org.apache.axis.encoding.ser.BeanSerializerFactory(
Order.class, qn),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(
Order.class, qn));
String result;
try {
//call.setTargetEndpointAddress(new java.net.URL(options.getURL()));
call.setTargetEndpointAddress(new java.net.URL(urlWsdl));
call.setOperationName(new QName("OrderProcessor", "processOrder"));
call.addParameter("arg1", qn, ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
result = (String) call.invoke(new Object[] { order });
} catch (AxisFault fault) {
result = "Error : " + fault.toString();
}
System.out.println(result);
}
}
********************************》*
oK! 到此,一个完整的Webservice例子就搞定了。
另外,Axis 还提供了一些工具,如:" wsdl2java "、" java2wsdl "
处理的方式与方法:
java2wsdl:
1. 以上为例,在以上工程的class输出目录新建一BAT文件:名为:“java2wsdl.bat”
2. 其内容为:
* java2wsdl.bat *
*《********************************
set AXIS_LIB=C:\axis-1_4\lib
SET CLASSPATH=.;%CLASSPATH%;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\axis.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar
java org.apache.axis.wsdl.Java2WSDL -o Order.wsdl -l"http://localhost:8080/axis/services/OrderProcessor" -n "urn:BeanService" -p"samples.userguide.example5.Order" "urn:Order" samples.userguide.example5.BeanService
********************************》*
把AXIS_LIB设为正确的本地包路径,再运行“java2wsdl.bat”,
便可以在目录下看到一个文件:“Order.wsdl”,通过这个WSDL文件,可以生成Webservice接口服务的框架,
既可以是服务器端,也可以是客户端框架。其细节,可以参考下一节。
处理的方式与方法:
wsdl2java :
1. 以上为例,把Order.wsdl拷贝过来,在其目录下新建一BAT文件:名为:“wsdl2java.bat”
2. 其内容为:
* wsdl2java.bat *
*《********************************
set AXIS_LIB=C:\axis-1_4\lib
SET CLASSPATH=.;%CLASSPATH%;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\axis.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar
java org.apache.axis.wsdl.WSDL2Java Order.wsdl
********************************》*
或者有另外的作法:把服务端的应用启动,直接写成:
“
java org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/services/OrderProcessor?wsdl
”
保存好后,运行这个BAT文件,
便会自动生成:
BeanService_pkg\
BeanService.java
BeanServiceService.java
BeanServiceServiceLocator.java
OrderProcessorSoapBindingStub.java
Order.java
ok.