最近工作用到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,上网查过,但没查出所以然。。。 求真相!!!
- 大小: 39.2 KB
分享到:
相关推荐
Spring+Axis2实例、简单、单纯,干净的WebService附带jar包可以直接安装允许
spring+axis集成webservice
Spring+axis2开发webservice[整理].pdf
一个spring+axis开发webservice的完整例子,还配有jdom解析xml
Spring 集成 AXIS2 的 Web service 配置方法 Spring 是一个流行的 Java 应用程序...总结来说,Spring 集成 AXIS2 的 Web 服务配置方法可以提高开发效率和系统可维护性,该配置方法可以应用于企业级应用程序的开发中。
NULL 博文链接:https://laochake.iteye.com/blog/445833
cxf-2.4.1+axis-1.4,整合spring3.0所用jar包
简单例子:axis2整合spring发布webservice,以及session的管理
Web services是建立可互操作的分布式应用程序的新平台。Web service平台是一套标准,它定义了应用程序如何在Web上实现互操作性。你可以用任何你喜欢的语言,在任何...在这里主要介绍两种实现方式:axis2和xfire框架。
spring+axis实例,也是在网络不容易搜索到的,觉得不错拿来共享
已经通过测试,运行稳定,需要注意的是axis版本为1
初学axis2,这是我的第一个项目,比较简单,希望对大家有帮助。由于jar包太大,我没有权限上传,所以只截了张图在项目里。包里有struts2的包,放在里面没关系。里面默认的数据库是oracle,大家可以根据自己的情况换...
公司项目需要,搭建了SSM框架,当初想更好的熟悉SSM框架的架构及其原理,所以未使用maven,资源中包括所有的jar包,能直接运行在...过滤器,axis2 webservice, gson和json2,jquery1.11, ajax同步异步请求Demo等。
NULL 博文链接:https://gjp014.iteye.com/blog/2332863
包含axis2对map\list和普通java类的处理方式,以axiom和对象数组为处理方式,还有简单的异步、同步处理; 含aixs2-1.4.1的jar包,对初学AXIS2的人学习有帮助。
axis2+spring+ibatis整合的一个项目,含webservice文件传输
tomcat+https+axis2
spring+axis2整合,项目下载之后就能使用,里面简单的编写的发布服务的代码,适合初学者使用
企业级应用程序示例,包含所有jar包,调试完成,tomcat,wehsphere6跑都没问。
Eclipse + Axis2发布WebService