java隨記

          堅(jiān)持就是勝利!

           

          j2ee web service開(kāi)發(fā)(一) 映射數(shù)組復(fù)雜類型

          ?之所以把數(shù)組類型在調(diào)用web service中作為參數(shù)的實(shí)現(xiàn)鄭重的記錄下來(lái),實(shí)在是因?yàn)閿?shù)組在j2ee web service的實(shí)現(xiàn)不是一件容易的問(wèn)題。至少用Jbossj2ee web service實(shí)現(xiàn)時(shí)是這樣. 好象網(wǎng)上關(guān)于web service的比較深入一點(diǎn)的資料比較少,關(guān)于j2ee web service的資料就更少了. 這里只記錄了符合WS I BP 標(biāo)準(zhǔn)的SOAP四種消息傳遞模式中的RPC/Literal消息傳遞模式。這個(gè)請(qǐng)參考書(shū)籍《j2ee web service 高級(jí)編程》一書(shū)。該書(shū)也只能是參考了,只是介紹了web service的理論實(shí)現(xiàn)及一點(diǎn)jax-rpc API,并且還有蠻多印刷錯(cuò)字。雖然jax-rpc API的目標(biāo)之一就是為了在各種java web service實(shí)現(xiàn)技術(shù)中提供一個(gè)便攜的手段,然而各種j2ee服務(wù)器的web service技術(shù)還是有些差別的,這個(gè)還是造成了程序不能直接在各種j2ee服務(wù)器之間毫無(wú)修改的移植。在 apacheaxis實(shí)現(xiàn)中,數(shù)組作為參數(shù)傳遞根本不值一提,借助工具很方面的就可以實(shí)現(xiàn)。

          但是在jboss中的ws實(shí)現(xiàn)中確不是一件便利的事情。因?yàn)橛魫灹撕芫?,發(fā)點(diǎn)牢騷:)具體實(shí)現(xiàn):

          ??? 作為web service服務(wù)實(shí)現(xiàn)的類必須實(shí)現(xiàn)java.rmi.Remote的接口:

          package array;

          ?

          import java.rmi.Remote;

          import java.rmi.RemoteException;

          ?

          public interface CountUser extends Remote {

          ? ?? public int countUser(User[] user) throws RemoteException;

          }

          ?

          ??? 具體實(shí)現(xiàn)遠(yuǎn)程接口的類:

          package array;

          ?

          public class CountUserImpl implements CountUser {

          ??? public int countUser(User[] user){

          ??????? for(int i=0;i<user.length;i++){

          ??????????? System.out.println("name "+user[i].getName()+"? phone? "+

          ?????????????????????????????? user[i].getPhone()+ " birthday "+user[i].getBirthDay());

          ??????? }

          ??????? return user.length;

          ??? }

          ?

          }

          ?

          ? 作為數(shù)組參數(shù)傳遞的類:

          ?

          package array;

          ?

          import java.util.Date;

          ?

          public class User {

          ??? private String name;

          ??? private String phone;

          ??? private Date birthDay;

          ?

          ??? public User() {

          ?

          ??? }

          ??? public User(String name,String phone,Date birthDay){

          ??????? this.name=name;

          ??????? this.phone=phone;

          ??????? this.birthDay=birthDay;

          ??? }

          ?

          ??? public void setName(String name) {

          ??????? this.name = name;

          ??? }

          ?

          ??? public void setPhone(String phone) {

          ??????? this.phone = phone;

          ??? }

          ?

          ??? public void setBirthDay(Date birthDay) {

          ??????? this.birthDay = birthDay;

          ??? }

          ?

          ??? public String getName() {

          ? ??????return name;

          ??? }

          ?

          ??? public String getPhone() {

          ??????? return phone;

          ??? }

          ?

          ??? public Date getBirthDay() {

          ??????? return birthDay;

          ??? }

          ?

          }

          ?

          使用Jboss_Home/bin 下的wstools工具生成布署j2ee web service 必須的三個(gè)文件,jax-rpc映射文件,web服務(wù)描述器文件,以及wsdl文檔用于wstools工具的配置文件jboss-config.xml類容如下:

          <?xml version="1.0" encoding="UTF-8"?>

          <configuration xmlns="http://www.jboss.org/jbossws-tools"

          ?? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

          ?? xsi:schemaLocation="http://www.jboss.org/jbossws-tools http://www.jboss.org/jbossws-tools/schema/jbossws-tool_1_0.xsd"

          ? <java-wsdl>

          ? <service? name="ServiceBeanInterface1" style="rpc"?? endpoint="array.ServiceBeanInterface1"/>

          ????? <namespaces target-namespace="http://array" type-namespace="http://array"/>

          ???? <mapping file="ServiceBeanInterface1.xml"/>

          ???? <webservices servlet-link="ServiceBeanInterface1"/>?????

          ?

          </java-wsdl>

          </configuration>

          使用 wstools -cp array.CountUser -config jboss-config.xml 命令可以生成布署web服務(wù)所需的三個(gè)文件。另外還得在web.xml文件中把CountUser接口作為servlet發(fā)布

          ? <servlet>

          ??? <display-name>CountUser Servlet</display-name>

          ??? <servlet-name>CountUser</servlet-name>

          ??? <servlet-class>array.CountUserImpl</servlet-class>

          ? </servlet>

          ? <servlet-mapping>

          ??? <servlet-name>CountUser</servlet-name>

          ??? <url-pattern>/CountUser</url-pattern>

          ? </servlet-mapping>

          ? <servlet-mapping>

          ??? <servlet-name>CountUser</servlet-name>

          ??? <url-pattern>/services/*</url-pattern>

          ? </servlet-mapping>

          僅僅是這樣倒也算很容易了,但是wstools工具并不能在映射文件中正確實(shí)現(xiàn)User[]的映射。因此還需要手工添加如下代碼以實(shí)現(xiàn)User[] xml之間的映射。???

          ??? <java-xml-type-mapping>

          ??? <java-type>array.User[]</java-type>

          ??? <root-type-qname xmlns:typeNS='http://array'>typeNS:User.Array</root-type-ame>

          ??? <qname-scope>complexType</qname-scope>

          ??? </java-xml-type-mapping>

          打包成war文件,并且布署。服務(wù)端的實(shí)現(xiàn)就完成了。打包的war文件下載 ?使用的Jboss版本4.04
          因?yàn)槠蛟S還有分類的原因,客戶端的實(shí)現(xiàn)將記錄在下一篇隨筆里 如果你想賜教或者討論,歡迎加入QQ群:30406099

          posted on 2006-11-06 09:33 傻 瓜 閱讀(4161) 評(píng)論(0)  編輯  收藏 所屬分類: j2ee web service

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(7)

          我參與的團(tuán)隊(duì)

          隨筆分類

          隨筆檔案

          文章分類

          友情鏈接

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 河池市| 锦州市| 岳池县| 额尔古纳市| 嘉荫县| 乡宁县| 子长县| 林西县| 东兰县| 墨脱县| 江华| 桦南县| 乌兰浩特市| 安化县| 灵川县| 中牟县| 婺源县| 昭平县| 百色市| 吉水县| 海安县| 木里| 水富县| 团风县| 深州市| 化州市| 安溪县| 逊克县| 三原县| 杭州市| 株洲县| 张家川| 嘉定区| 色达县| 和龙市| 县级市| 喀什市| 张家港市| 苍山县| 色达县| 新兴县|