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 阅读(4605) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: