`
zhuyuanxiang
  • 浏览: 127184 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

webservice-之使用axis+spring开发(转自勇哥的BLOG)

阅读更多

一、环境配置 :在 eclipse 中配置引入相应的 Spring 框架( core/Remoting/Web )、 axis 包。 <!----> <o:p> </o:p>

<!----> <v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"> <v:stroke joinstyle="miter"> </v:stroke> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"> </v:f> <v:f eqn="sum @0 1 0"> </v:f> <v:f eqn="sum 0 0 @1"> </v:f> <v:f eqn="prod @2 1 2"> </v:f> <v:f eqn="prod @3 21600 pixelWidth"> </v:f> <v:f eqn="prod @3 21600 pixelHeight"> </v:f> <v:f eqn="sum @0 0 1"> </v:f> <v:f eqn="prod @6 1 2"> </v:f> <v:f eqn="prod @7 21600 pixelWidth"> </v:f> <v:f eqn="sum @8 21600 0"> </v:f> <v:f eqn="prod @7 21600 pixelHeight"> </v:f> <v:f eqn="sum @10 21600 0"> </v:f> </v:formulas> <v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"> </v:path> <o:lock aspectratio="t" v:ext="edit"> </o:lock> </v:shapetype> <v:shape id="_x0000_i1025" style="WIDTH: 414.75pt; HEIGHT: 273pt" type="#_x0000_t75"> <v:imagedata o:title="" src="file:///D:\Winbak\Temp\msohtml1\01\clip_image001.png"> </v:imagedata> </v:shape> <o:p> </o:p>

<o:p> </o:p>

二、代码开发 <o:p> </o:p>

1、  MyEclipse 中建立一个新的 J2EE Web Project, 新建 java test <o:p> </o:p>

2、  接口文件 HelloWorldRemote.java<o:p></o:p>

package test;<o:p></o:p>

//Spring 工程中要使用的接口文件 <o:p> </o:p>

public interface HelloWorldRemote <o:p></o:p>

{<o:p></o:p>

       public String getMessage(String name);<o:p></o:p>

}<o:p></o:p>

3、  接口实现文件 HelloWorldBean.java<o:p></o:p>

package test;<o:p></o:p>

//Spring 工程中要使用的接口实现文件 <o:p> </o:p>

public class HelloWorldBean implements HelloWorldRemote <o:p></o:p>

{     <o:p></o:p>

       private String helloStr; // Spring 中需要注入的字符串        <o:p> </o:p>

       public String getHelloStr() <o:p></o:p>

       {<o:p></o:p>

              return helloStr;<o:p></o:p>

       }<o:p></o:p>

       public void setHelloStr(String helloStr) <o:p></o:p>

       {<o:p></o:p>

              this.helloStr = helloStr;<o:p></o:p>

       }<o:p></o:p>

       // 实现接口中的方法 <o:p> </o:p>

       public String getMessage(String name) <o:p></o:p>

       {<o:p></o:p>

              return helloStr + ":" + name;<o:p></o:p>

       }     <o:p></o:p>

}<o:p></o:p>

4、  Spring 中对 Web Service 进行封装很简单,仅仅需要继承 <o:p> </o:p>

org.springframework.remoting.jaxrpc.ServletEndpointSupport 类,实现里面的一些方法,包装一次,将其发布出来就可以。 HelloWorldWebService.java<o:p></o:p>

package test;<o:p></o:p>

import javax.xml.rpc.ServiceException;<o:p></o:p>

import org.springframework.remoting.jaxrpc.ServletEndpointSupport;<o:p></o:p>

public class HelloWorldWebService <o:p></o:p>

              extends ServletEndpointSupport <o:p></o:p>

              implements HelloWorldRemote <o:p></o:p>

{<o:p></o:p>

       private HelloWorldRemote helloWorld;<o:p></o:p>

       protected void onInit() throws ServiceException <o:p></o:p>

       {<o:p></o:p>

              // Spring 容器中获取 Bean 的实例 <o:p> </o:p>

              helloWorld = (HelloWorldRemote) getApplicationContext()<o:p></o:p>

                            .getBean("myHelloWorldBean");<o:p></o:p>

       }<o:p></o:p>

       public String getMessage(String name)  <o:p></o:p>

       {<o:p></o:p>

              // 执行 Bean 中的相同的方法 <o:p> </o:p>

              return helloWorld.getMessage(name);<o:p></o:p>

       }<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

三、配置文件 (全部放在 /WEB-INF/ 目录下 <o:p> </o:p>

1、  web.xml web 加载 spring axis 配置 <o:p> </o:p>

<!--Spring 框架需要引入的配置文件及相关类 --><o:p></o:p>

       <context-param><o:p></o:p>

              <param-name>contextConfigLocation</param-name><o:p></o:p>

              <param-value>/WEB-INF/applicationContext.xml</param-value><o:p></o:p>

       </context-param><o:p></o:p>

       <servlet><o:p></o:p>

              <servlet-name>context</servlet-name><o:p></o:p>

              <servlet-class><o:p></o:p>

                     org.springframework.web.context.ContextLoaderServlet<o:p></o:p>

              </servlet-class><o:p></o:p>

              <load-on-startup>1</load-on-startup><o:p></o:p>

       </servlet><o:p></o:p>

       <!--axis 需要引入的 Servlet --><o:p></o:p>

       <servlet><o:p></o:p>

              <servlet-name>axis</servlet-name><o:p></o:p>

              <servlet-class><o:p></o:p>

                     org.apache.axis.transport.http.AxisServlet<o:p></o:p>

              </servlet-class><o:p></o:p>

              <load-on-startup>2</load-on-startup><o:p></o:p>

       </servlet><o:p></o:p>

       <servlet-mapping><o:p></o:p>

              <servlet-name>axis</servlet-name><o:p></o:p>

              <url-pattern>/services/*</url-pattern><o:p></o:p>

       </servlet-mapping><o:p></o:p>

       <!--axis Web Service Web 发布路径 --><o:p></o:p>

2、  applicationContext.xml spring 的配置 <o:p> </o:p>

<?xml version="1.0" encoding="UTF-8"?>   <o:p></o:p>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" <o:p></o:p>

"http://www.springframework.org/dtd/spring-beans.dtd">     <o:p></o:p>

<beans><o:p></o:p>

<bean id="myHelloWorldBean" class="test.HelloWorldBean"><o:p></o:p>

              <property name="helloStr"><o:p></o:p>

                     <value>Say Hello to :</value><o:p></o:p>

              </property><o:p></o:p>

       </bean><o:p></o:p>

</beans><o:p></o:p>

3、  server-config.wsdd axis 服务配置 <o:p> </o:p>

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

       xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"><o:p></o:p>

       <handler name="URLMapper"<o:p></o:p>

              type="java:org.apache.axis.handlers.http.URLMapper" />    <o:p></o:p>

       <!-- 系统服务 --><o:p></o:p>

       <service name="AdminService" provider="java:MSG"><o:p></o:p>

              <parameter name="allowedMethods" value="AdminService" /><o:p></o:p>

              <parameter name="enableRemoteAdmin" value="false" /><o:p></o:p>

              <parameter name="className" value="org.apache.axis.utils.Admin" /><o:p></o:p>

              <namespace>http://xml.apache.org/axis/wsdd/</namespace><o:p></o:p>

       </service><o:p></o:p>

       <service name="Version" provider="java:RPC"><o:p></o:p>

              <parameter name="allowedMethods" value="getVersion" /><o:p></o:p>

              <parameter name="className" value="org.apache.axis.Version" /><o:p></o:p>

       </service>      <o:p></o:p>

       <!-- 自定义服务 --><o:p></o:p>

       <service name="myWebService" provider="java:RPC"><o:p></o:p>

              <parameter name="className"<o:p></o:p>

                     value="test.HelloWorldWebService" /><o:p></o:p>

              <parameter name="allowedMethods" value="*" /><o:p></o:p>

       </service><o:p></o:p>

       <transport name="http"><o:p></o:p>

              <requestFlow><o:p></o:p>

                     <handler type="URLMapper" /><o:p></o:p>

              </requestFlow><o:p></o:p>

       </transport><o:p></o:p>

</deployment><o:p></o:p>

<o:p> </o:p>

四、测试 客户端 TestWebServiceClient.java<o:p></o:p>

package test;<o:p></o:p>

import javax.xml.namespace.QName;<o:p></o:p>

import org.apache.axis.client.Call;<o:p></o:p>

import org.apache.axis.client.Service;<o:p></o:p>

public class TestWebServiceClient <o:p></o:p>

{     <o:p></o:p>

       public static void main(String[] args)<o:p></o:p>

       {     <o:p></o:p>

              try <o:p></o:p>

              {<o:p></o:p>

                     String wsdlUrl <o:p></o:p>

= "http://localhost:8080/spring-axis/services/myWebService?wsdl";<o:p></o:p>

                     String nameSpaceUri <o:p></o:p>

= "http://localhost:8080/spring-axis/services/myWebService";<o:p></o:p>

                     // 创建调用对象 <o:p> </o:p>

                     Service service = new Service();<o:p></o:p>

                     Call call = null;<o:p></o:p>

                     call = (Call) service.createCall();<o:p></o:p>

                     // 调用 getMessage<o:p></o:p>

                     System.out.println(">>>getMessage");<o:p></o:p>

                     call.setOperationName(new QName(nameSpaceUri, "getMessage"));<o:p></o:p>

                     call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));<o:p></o:p>

                     String ret = (String) call.invoke(new Object[] { "ABC" });<o:p></o:p>

                     System.out.println("return value is " + ret);<o:p></o:p>

              } <o:p></o:p>

              catch (Exception e) <o:p></o:p>

              {<o:p></o:p>

                     e.printStackTrace();<o:p></o:p>

              }<o:p></o:p>

       }<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics