最近工作用到spring+axis,现在抽空整理一下,由于实际工作中仍有细节没有弄清楚,在此只总结一下一般流程,分享给大家,希望对你有帮助,如果能解答我的疑惑还望指教。在网上查了很多资料,大多数是使用Myeclipse,出于个人喜好,我使用的IDE是Eclipse+tomcat6.0。
步骤1:在Eclipse中建立一个web项目,这里起名叫AxisTest,需要的jar包有:
commons-discovery.jar、jaxrpc.jar、saaj.jar、wsdl4j.jar、axis.jar、commons-logging.jar、spring.jar,由于网上大多数例子都是用MyEclipse自动导入jar,所以有的文章中介绍还需要activation.jar、ant.jar,但是在项目把这两个jar包去掉,照样跑通,所以此处不明白这个两个jar有何用,有谁知道请留言给我,谢谢。
步骤2:导入jar包后,分别写如下代码:
声明接口
package com.webservice.axis.test;
/**
* 声明方法接口
*/
public interface IHelloWorld {
public String getMessage(String word);
}
实现类
package com.webservice.axis.test;
/**
* 实现接口的类,Ioc的依赖注入可以在此处自由发挥
*/
public class HelloWorldImpl implements IHelloWorld {
private String helloStr; // Spring 中需要注入的字符串
public String getHelloStr() {
return helloStr;
}
public void setHelloStr(String helloStr) {
this.helloStr = helloStr;
}
// 实现接口中的方法
@Override
public String getMessage(String name) {
return helloStr + ":" + name;
}
}
下面一个类,感觉上像是spring和axis的结合点,用来将类发布成webService,但有些地方值得说一下。
package com.webservice.axis.test;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
/**
* ServletEndpointSupport 是spring提供支持WS的类,这里只需要继承,但是这个类是依赖jaxrpc.jar包,
* 而从Apache下载的axis1.4中并没有找到jaxrpc.jar包,网上查了很多资料,貌似jaxrpc.jar现在被
* jaxws-1.4.1.jar代替,曾试过将jaxws导入项目,倒是ServletEndpointSupport 仍然不支持,难
* 道spring没有对jaxws的支持?
*/
public class HelloWorldWebService extends ServletEndpointSupport implements IHelloWorld {
private HelloWorldImpl helloWorld;
protected void onInit()throws ServiceException {
// 在 Spring 容器中获取 Bean 的实例
helloWorld = (HelloWorldImpl) getApplicationContext()
.getBean("myHelloWorldBean");
}
@Override
public String getMessage(String name) {
// 执行 Bean 中的相同的方法
return helloWorld.getMessage(name);
}
}
步骤3:配置web.xml,这个没什么好说的
<!--Spring 框架需要引入的配置文件及相关类 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*: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> <!--axis 的 Web Service 的 Web 发布路径 --> <servlet-mapping> <servlet-name>axis</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
步骤4:配置server-config.wsdd
<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="com.webservice.axis.test.HelloWorldWebService" /> <parameter name="allowedMethods" value="*" /> <parameter name="scope" value="request" /> </service> <transport name="http"> <requestFlow> <handler type="URLMapper" /> </requestFlow> </transport> </deployment>
步骤5:配置applicationContext
<beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop ="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="myHelloWorldBean" class="com.webservice.axis.test.HelloWorldImpl"> <property name="helloStr" value="Say Hello to :" /> </bean> </beans>
项目目录为:
这样服务就发布了,你可以访问http://localhost:8080/AxisTest/services/myWebService或者访问http://localhost:8080/AxisTest/services/myWebService?wsdl来查看生成的myWebService.wsdl文件
有几点想和大家讨论一下:
第一:
在HelloWorldWebService 中helloWorld是通过下面一句,实现注入的,个人感觉不是很优雅
helloWorld = (HelloWorldImpl) getApplicationContext().getBean("myHelloWorldBean");
于网上查了不少资料,有一种说法是在server-config.wsdd中将配置改成
<!-- 自定义服务 --> <service name="myWebService" provider="java:SPRING"> <parameter name="className" value="Spring中Bean的名字" /> <parameter name="allowedMethods" value="*" /> <parameter name="scope" value="request" /> </service>
但是这样做会出异常,查了一些资料得知provider的服务类型有4种:RPC、Document、Wrapped和Message,是org.apache.axis.providers包中的API提供的支持,不清楚上面java:SPRING是怎么出来的,或许是我哪里配错了?
第二、当访问http://localhost:8080/AxisTest/services/myWebService.wsdl时,会出现警告
警告: The class com.webservice.axis.test.IHelloWorld does not contain a default constructor, which is a requirement for a bean class. The class cannot be converted into an xml schema type. An xml schema anyType will be used to define this class in the wsdl file.
目前解决方法是将HelloWorldWebService中的helloWorld私有变量声明称HelloWorldImpl类型,为什么有这种情况不得而知,如果哪位牛人经过指点迷津。
第三、就是用JDK6自带的一个WSImport工具根据wsdl生成客户端代码,具体的操作如下:
1、首先要交代WSImport.exe的位置,他在JDK安装目录的bin文件下,cmd可以直接使用%JAVA_HOME%"\bin\WSImport调用
2、打开cmd命令行,输入指令wsimport -s filePath http://localhost:8080/AxisTest/services/myWebService.wsdl
说明: wsimport -s 生成代码保存路径 wsdl文件路径 详情参见http://www.blogjava.net/downmoon/archive/2010/08/29/330136.html
上面的做法是正确无误的,但是我试过会出异常[ERROR] rpc/encoded wsdls are not supported in JAXWS 2.0,上网查过,但没查出所以然。。。 求真相!!!