Java, Only Java!

          統計

          留言簿(20)

          積分與排名

          好友空間

          文檔技巧

          閱讀排行榜

          評論排行榜

          webservice-之使用axis+spring開發(轉自勇哥的BLOG)

          一、環境配置 :在 eclipse 中配置引入相應的 Spring 框架( core/Remoting/Web )、 axis 包。

          ?

          二、代碼開發

          1、? MyEclipse 中建立一個新的 J2EE Web Project, 新建 java test

          2、? 接口文件 HelloWorldRemote.java

          package test;

          //Spring 工程中要使用的接口文件

          public interface HelloWorldRemote

          {

          ?????? public String getMessage(String name);

          }

          3、? 接口實現文件 HelloWorldBean.java

          package test;

          //Spring 工程中要使用的接口實現文件

          public class HelloWorldBean implements HelloWorldRemote

          {????

          ?????? private String helloStr; // Spring 中需要注入的字符串 ??????

          ?????? public String getHelloStr()

          ?????? {

          ????????????? return helloStr;

          ?????? }

          ?????? public void setHelloStr(String helloStr)

          ?????? {

          ????????????? this.helloStr = helloStr;

          ?????? }

          ?????? // 實現接口中的方法

          ?????? public String getMessage(String name)

          ?????? {

          ????????????? return helloStr + ":" + name;

          ?????? }????

          }

          4、? Spring 中對 Web Service 進行封裝很簡單,僅僅需要繼承

          org.springframework.remoting.jaxrpc.ServletEndpointSupport 類,實現里面的一些方法,包裝一次,將其發布出來就可以。 HelloWorldWebService.java

          package test;

          import javax.xml.rpc.ServiceException;

          import org.springframework.remoting.jaxrpc.ServletEndpointSupport;

          public class HelloWorldWebService

          ????????????? extends ServletEndpointSupport

          ????????????? implements HelloWorldRemote

          {

          ?????? private HelloWorldRemote helloWorld;

          ?????? protected void onInit() throws ServiceException

          ?????? {

          ????????????? // Spring 容器中獲取 Bean 的實例

          ????????????? helloWorld = (HelloWorldRemote) getApplicationContext()

          ??????????????????????????? .getBean("myHelloWorldBean");

          ?????? }

          ?????? public String getMessage(String name)?

          ?????? {

          ????????????? // 執行 Bean 中的相同的方法

          ????????????? return helloWorld.getMessage(name);

          ?????? }

          }

          ?

          三、配置文件 (全部放在 /WEB-INF/ 目錄下

          1、? web.xml web 加載 spring axis 配置

          <!--Spring 框架需要引入的配置文件及相關類 -->

          ?????? <context-param>

          ????????????? <param-name>contextConfigLocation</param-name>

          ????????????? <param-value>/WEB-INF/applicationContext.xml</param-value>

          ?????? </context-param>

          ?????? <servlet>

          ????????????? <servlet-name>context</servlet-name>

          ????????????? <servlet-class>

          ???????????????????? org.springframework.web.context.ContextLoaderServlet

          ????????????? </servlet-class>

          ????????????? <load-on-startup>1</load-on-startup>

          ?????? </servlet>

          ?????? <!--axis 需要引入的 Servlet -->

          ?????? <servlet>

          ????????????? <servlet-name>axis</servlet-name>

          ????????????? <servlet-class>

          ???????????????????? org.apache.axis.transport.http.AxisServlet

          ????????????? </servlet-class>

          ????????????? <load-on-startup>2</load-on-startup>

          ?????? </servlet>

          ?????? <servlet-mapping>

          ????????????? <servlet-name>axis</servlet-name>

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

          ?????? </servlet-mapping>

          ?????? <!--axis Web Service Web 發布路徑 -->

          2、? applicationContext.xml spring 的配置

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

          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

          "http://www.springframework.org/dtd/spring-beans.dtd">????

          <beans>

          <bean id="myHelloWorldBean" class="test.HelloWorldBean">

          ????????????? <property name="helloStr">

          ???????????????????? <value>Say Hello to :</value>

          ????????????? </property>

          ?????? </bean>

          </beans>

          3、? server-config.wsdd axis 服務配置

          <deployment xmlns="http://xml.apache.org/axis/wsdd/"

          ?????? xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

          ?????? <handler name="URLMapper"

          ????????????? type="java:org.apache.axis.handlers.http.URLMapper" />???

          ?????? <!-- 系統服務 -->

          ?????? <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="myWebService" provider="java:RPC">

          ????????????? <parameter name="className"

          ???????????????????? value="test.HelloWorldWebService" />

          ????????????? <parameter name="allowedMethods" value="*" />

          ?????? </service>

          ?????? <transport name="http">

          ????????????? <requestFlow>

          ???????????????????? <handler type="URLMapper" />

          ????????????? </requestFlow>

          ?????? </transport>

          </deployment>

          ?

          四、測試 客戶端 TestWebServiceClient.java

          package test;

          import javax.xml.namespace.QName;

          import org.apache.axis.client.Call;

          import org.apache.axis.client.Service;

          public class TestWebServiceClient

          {????

          ?????? public static void main(String[] args)

          ?????? {????

          ????????????? try

          ????????????? {

          ???????????????????? String wsdlUrl

          = "http://localhost:8080/spring-axis/services/myWebService?wsdl";

          ???????????????????? String nameSpaceUri

          = "http://localhost:8080/spring-axis/services/myWebService";

          ???????????????????? // 創建調用對象

          ???????????????????? Service service = new Service();

          ???????????????????? Call call = null;

          ???????????????????? call = (Call) service.createCall();

          ???????????????????? // 調用 getMessage

          ???????????????????? System.out.println(">>>getMessage");

          ???????????????????? call.setOperationName(new QName(nameSpaceUri, "getMessage"));

          ???????????????????? call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));

          ???????????????????? String ret = (String) call.invoke(new Object[] { "ABC" });

          ???????????????????? System.out.println("return value is " + ret);

          ????????????? }

          ????????????? catch (Exception e)

          ????????????? {

          ???????????????????? e.printStackTrace();

          ????????????? }

          ?????? }

          }

          ?

          posted on 2008-03-03 09:51 zYx.Tom 閱讀(4641) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 红桥区| 合江县| 水城县| 房产| 巴林左旗| 桦川县| 宁乡县| 大城县| 禄丰县| 恩施市| 柘城县| 灌云县| 奇台县| 沅江市| 屏山县| 博湖县| 绥中县| 西平县| 宁乡县| 罗山县| 黄山市| 望江县| 扎兰屯市| 曲周县| 江阴市| 金坛市| 祁连县| 乐都县| 灵台县| 电白县| 安吉县| 微博| 昔阳县| 资阳市| 临沭县| 乌什县| 抚州市| 岚皋县| 调兵山市| 九江县| 绿春县|