這幾天也一直在為了設(shè)計與實現(xiàn)一個業(yè)務(wù)平臺,想用Webservice 來作為核心技術(shù)來實現(xiàn),也一直想精溢求精地把這個東東搞好, 突然覺得好久沒來這里寫日志了.呵,時間如白駒過隙啊!
對于webService 以前也接觸過.但只是皮毛,這次可是來真格的.來就來吧,年輕人這就是機(jī)會.一定得把握好,干它一票! 呵,就在網(wǎng)上到處找找啊,在本地機(jī)器上左試試,右試試啊, 什么法子都用一遍。就不信搞不定它,小樣!
網(wǎng)上的例子也比較散,比較簡易的,達(dá)不我想要的業(yè)務(wù)水平,只能慢慢搜索與摸索吧。。。。。。就這樣天天想著這事,也沒什么心情來這里寫寫。
近些天,也快回深圳了,定在下周四動身吧。這邊的工作也得告一節(jié)了。把我前一些日子,搞一個完整的Samples 放到這里來,與大家共享。希望對有心人有些啟發(fā),也希望高人指點與點評。。。。。。。、
****************************************************
*Axis開發(fā)Webservice傳Bean對象返回String串的實例.rtf*
****************************************************
在ECLIPSE 里新建一工程,其次建包,再次建類,
基本包:samples.userguide.example5
基本類: Order.java
內(nèi)容:
* 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;
}
}
********************************》*
主服務(wù)包: samples.userguide.example5
主服務(wù)類: 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()),來測試此應(yīng)用的可運(yùn)行性;
到此,服務(wù)端類寫完了,接著來把此類的方法發(fā)布成 WebService 接口
手寫一個deploy.wsdd 文件,可以參考下面的例子:
*deploy.wsdd例子*
*《********************************
<?xml version="1.0" encoding="utf-8"?>
<deployment xmlns="<parameter name="className" value="服務(wù)類文件的實際路徑(包括包的信息)"/>
<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=" <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= “應(yīng)用發(fā)布的路徑”
set AXIS_LIB= “AXIS框架的內(nèi)部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服務(wù)名\services\AdminService -p端口號(如果你的端口號不是默認(rèn)的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
********************************》*
保存好,接下來架設(shè)Webservice.
環(huán)境架設(shè):
首先,安裝JDK...(略)...本例用JDK版本號V1.4.
其次,安裝Tomcat...(略)...本例用Tomcat版本號V4.1.
再次,AXIS...(略)...本例用AXIS版本號V1.4. 去AXIS官方網(wǎng)站: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)下,
架設(shè)AXIS:
首先,將deploy.wsdd文件Copy到 %AXISCLASSPATH%/WEB-INF 目錄下;
其次,將之前的類編譯后,連路徑一起Copy到 %AXISCLASSPATH%/WEB-INF/classes目錄下,
最后,將tomcat的 catalina 服務(wù)啟動.
(即:C:\Axis_webservice\Tomcat_4.1\bin,
[特別注意的是:]
A。tomcat務(wù)必啟動。
B。在寫如上的命令行時,務(wù)必注意參數(shù)的寫法。“-D” 與 “-S”與其值間不能有空格。切記!
C。各XX的路徑里,最好是不要有空格,因為JAVA里對大小寫與路徑空格有校驗。
,)
測試:
命令行啟動方法;運(yùn)行"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" 便可看到默認(rèn)情況下發(fā)布了兩個服務(wù)下各有一個方法。一切準(zhǔn)備工作已經(jīng)OK!
再回到,剛才我們寫的那個批處理文件:" makeWsdd.bat",
直接雙擊,便可執(zhí)行, 有一個警告,可以忽略的。執(zhí)行完成后,在相應(yīng)的
%AXISCLASSPATH%/WEB-INF下便有一“server-config.wsdd”文件,如下:
*server-config.wsdd*
*《********************************
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns=" <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",或者,再刷新。
便可以看到,我們新發(fā)布的一個webservice接口服務(wù),其名為:"OrderProcessor "
此服務(wù)下的個方法:其名為:"processOrder "。
點擊服務(wù)對應(yīng)的"(WSDL)"可見服務(wù)WSDL描述內(nèi)容。在其中也可以得服務(wù)的訪問地址:
"http://localhost:8080/axis/services/OrderProcessor?wsdl"
服務(wù)器端的開發(fā),搞定了!
-:) -:) -:)
接下來,準(zhǔn)備開發(fā),客戶端的開發(fā)。
* 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. 其內(nèi)容為:
* 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設(shè)為正確的本地包路徑,再運(yùn)行“java2wsdl.bat”,
便可以在目錄下看到一個文件:“Order.wsdl”,通過這個WSDL文件,可以生成Webservice接口服務(wù)的框架,
既可以是服務(wù)器端,也可以是客戶端框架。其細(xì)節(jié),可以參考下一節(jié)。
處理的方式與方法:
wsdl2java :
1. 以上為例,把Order.wsdl拷貝過來,在其目錄下新建一BAT文件:名為:“wsdl2java.bat”
2. 其內(nèi)容為:
* 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
********************************》*
或者有另外的作法:把服務(wù)端的應(yīng)用啟動,直接寫成:
“
java org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/services/OrderProcessor?wsdl
”
保存好后,運(yùn)行這個BAT文件,
便會自動生成:
BeanService_pkg\
BeanService.java
BeanServiceService.java
BeanServiceServiceLocator.java
OrderProcessorSoapBindingStub.java
Order.java
ok.
來自:http://blackskin126.bokee.com/viewdiary.14630183.html