背景:
运行在两台独立服务器上的两个web服务,其中一个web服务需要访问另一个web服务的业务方法。但是两个服务各自的运行环境不同,一个服务运行在jdk1.5上(服务端),使用spring2.5.6框架管理bean,而另一个运行在jdk1.4环境下(客户端)。现在要求通过webservice技术让客户端能访问服务端提供的webservice服务。
方案:
服务端因为使用了spring,故使用Apache CXF框架将业务bean的方法发布为webservice服务 ,客户端使用支持jdk1.4的Axis2-1.4.1,生成服务端业务方法的客户端存根,提供客户端使用。
备注:此方案对保证服务安全尚未有良好解决方案,对安全有要求请不要参考!
环境
IDE:Eclipse3.5
服务端:jdk1.5 , CXF2.2.4 (http://cxf.apache.org/download.html ), spring2.5.6 ,Tomcat6.0
客户端:jdk1.4 , Axis2-1.4.1( http://ws.apache.org/axis2/download/1_4_1/download.cgi )
代码生成工具:axis2-eclipse-codegen-wizard.zip和axis2-eclipse-service-archiver-wizard.zip,下载地址http://apache.justdn.org/ws/axis2/tools/1_4_1/
服务端:
发布一个webservice服务,
2.实体类Person.java
3.服务类和接口定义
接口 PersonService.java
类PersonServiceImpl.java
4.bean配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
xmlns:context="http://www.springframework.org/schema/context "
xmlns:aop="http://www.springframework.org/schema/aop "
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<context:annotation-config/>
<bean id="personService" class="com.webservice.service.impl.PersonServiceImpl"/>
</beans>
5.cxf配置文件
applicationContext-cxf.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
xmlns:jaxws="http://cxf.apache.org/jaxws "
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd "
default-autowire="byName" default-lazy-init="true">
<import resource="classpath:meta-inf/cxf/cxf.xml" />
<import resource="classpath:meta-inf/cxf/cxf-servlet.xml" />
<import resource="classpath:meta-inf/cxf/cxf-extension-soap.xml" />
<bean id="jaxWsServicefactorybean" class="org.apache.cxf.jaxws.support.JaxWsServicefactorybean" scope="prototype">
<property name="wrapped" value="true"/>
<property name="dataBinding" ref="aegisBean"/>
<property name="serviceConfigurations">
<list>
<bean class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration"/>
<bean class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration"/>
<bean class="org.apache.cxf.service.factory.DefaultServiceConfiguration"/>
</list>
</property>
</bean>
<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding"/>
<jaxws:endpoint id="WSpersonService" implementor="#personService" address="/PersonService">
<jaxws:serviceFactory>
<ref bean="jaxWsServicefactorybean"/>
</jaxws:serviceFactory>
</jaxws:endpoint>
</beans>
4.web.xml配置CXFServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " xmlns="http://java.sun.com/xml/ns/javaee " xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " id="WebApp_ID" version="2.5">
<display-name>CXFService</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.测试服务端
启动Tomcat,在浏览器地址栏输入:http://localhost:8080/CXFService/services ,看到如下页面
点击链接进入,可以查看服务的wsdl
至此服务发布成功!
客户端:(这里客户端只用java工程进行测试,web工程同理)
1.建立一个java project 命名为CxfServiceClient(如果需要在jdk1.4下运行,请将编译级别改为jdk1.4,否则忽略)
按照以下操作步骤进行:
选择Axis2 Code Generator,将PersonService的wsdl地址拷贝到以下输入框:
选择如下的参数:
继续点击Next,进入如下窗口,选择客户端工程根路径为代码生成路径,选择Axis2的lib导入到客户端工程(可选的步骤):
最后finish:
如果生成代码报错,请在插件中lib下加入stax-api-1.0.1.jar和backport-util-concurrent-3.1.jar,检查plugin.xml是否配置这两项,然后重启eclipse
代码生成完毕,这时会发现代码上有错误提示,原因是axis2的jar包还没有引入,将CxfServiceClient/lib下的jar包引入classpath即可解决此错误。
可以发现,针对List的数据类型,Axis2实际上是生成了ArrayOfPerson的类,将List转换成了对象数组进行处理!
3.写测试用例执行测试
在生成的测试代码上完善测试逻辑,将生成的test包拖拽到src下,完成两个测试方法的代码,示例:
执行测试,报错,
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source)
at java.lang.reflect.Method.invoke(UnkNown Source)
at test.com.webservice.service.impl.PersonServiceImplServiceTest.getTestObject(PersonServiceImplServiceTest.java:130)
at test.com.webservice.service.impl.PersonServiceImplServiceTest.testsave(PersonServiceImplServiceTest.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source)
at java.lang.reflect.Method.invoke(UnkNown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ExceptionInInitializerError
at com.webservice.service.impl.SaveDocument$Factory.newInstance(SaveDocument.java:45)
... 22 more
Caused by: java.lang.RuntimeException: Cannot load SchemaTypeSystem. Unable to load class with name schemaorg_apache_xmlbeans.system.s5EA05717581B4DB0D124E78F071F0690.TypeSystemHolder. Make sure the generated binary files are on the classpath.
at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:783)
at com.webservice.service.impl.SaveDocument.<clinit>(SaveDocument.java:20)
... 23 more
Caused by: java.lang.classNotFoundException: schemaorg_apache_xmlbeans.system.s5EA05717581B4DB0D124E78F071F0690.TypeSystemHolder
at java.net.urlclassloader$1.run(UnkNown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.urlclassloader.findClass(UnkNown Source)
at java.lang.classLoader.loadClass(UnkNown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(UnkNown Source)
at java.lang.classLoader.loadClass(UnkNown Source)
at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:769)
... 24 more
解决方案 :将生成的CxfServiceClient/resources/schemaorg_apache_xmlbeans拷贝到class编译路径下,我这里是bin目录下,如图,
再执行,提示成功!
参考文档:http://cxf.apache.org/docs/writing-a-service-with-spring.html
转载请注明出处http://blog.csdn.net/jack0511/archive/2009/11/06/4775691.aspx,谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。