集成还算是简单,就是在web.xml添加相关,spring配置就完事,具体如下:
1。web.xml
加入:
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>
org.codehaus.xfire.spring.XFireSpringServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!-- Spring config-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springbeans.xml</param-value>
<param-value>要包含xfire相关配置文件,或在其他配置文件中引入也可,如在其他配置文件中用
<import resource="applicationContext-webservice.xml" />引入
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.定义接口及实现服务
定义接口,这个接口中定义要通过WebService暴露的方法
package com.xxx.webservice;
public interface test {
public String sayHello(String name);
}
实现服务
package com.xx.webservice.impl;
import com.fireflow.webservice.test;
public class testImpl implements test {
public String sayHello(String name) {
return "Hello"+name;
}
}
3.配置服务
在spring引入的文件中(如applicationContext-webservice.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!--首先引入这个,这个以后可能抱错,见后面-->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<bean name="testService" class="org.codehaus.xfire.spring.ServiceBean">
<property name="serviceBean" ref="test"/>
<property name="serviceClass" value="com.xx.webservice.test"/>
<property name="inHandlers">
<list>
<ref bean="addressingHandler"/>
</list>
</property>
</bean>
<bean id="test" class="com.xx.webservice.impl.testImpl"/>
<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>
</beans>
OK完成了,测试一下:
通过IE,输入http://ip:port/context/services/BookService?wsdl出现相应的wsdl文档。
几个异常:
1.由于引入<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />报org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xfire.customEditorConfigurer' defined in ServletContext resource [/WEB-INF/customEditors.xml]: Cannot create inner bean 'org.codehaus.xfire.spring.editors.ServiceFactoryEditor#1022de4' while setting bean property 'customEditors' with key [org.codehaus.xfire.service.ServiceFactory]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.codehaus.xfire.spring.editors.ServiceFactoryEditor#1022de4' defined in ServletContext resource [/WEB-INF/customEditors.xml]: Cannot resolve reference to bean 'xfire.transportManager' while setting bean property 'transportManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xfire.transportManager' defined in ServletContext resource [/WEB-INF/xfire.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/stream/XMLStreamReader
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.codehaus.xfire.spring.editors.ServiceFactoryEditor#1022de4' defined in ServletContext resource [/WEB-INF/customEditors.xml]: Cannot resolve reference to bean 'xfire.transportManager' while setting bean property 'transportManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xfire.transportManager' defined in ServletContext resource [/WEB-INF/xfire.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/stream/XMLStreamReader
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xfire.transportManager' defined in ServletContext resource [/WEB-INF/xfire.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/stream/XMLStreamReader
Caused by:
java.lang.NoClassDefFoundError: javax/xml/stream/XMLStreamReader
at org.codehaus.xfire.soap.SoapTransportHelper.<clinit>(SoapTransportHelper.java:24)
at org.codehaus.xfire.transport.local.LocalTransport.<init>(LocalTransport.java:33)
解决方法:我是加入了jsr173_1.0_api.jar这个包,就解决了,这个包找下就成,也可在附件中可下
2.通过IE,输入http://ip:port/context/services/BookService?wsdl 测试服务正常,但是写了个jsp文件测试,报异常。
jsp文件内容:
try {
Client client = new Client(new URL("http://localhost:8888/xx/services/testService?wsdl"));
out.println("<b>1、</b>sayHello:");
Object[] results = client.invoke("sayHello", new Object[]{"Jim"});
out.println(results[0].toString());
out.println("<br>");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
所报异常是:
java.io.FileNotFoundException: http://localhost:8888/xx/services/testService?wsdl
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1168)
at java.net.URL.openStream(URL.java:1007)
at org.codehaus.xfire.client.Client.<init>(Client.java:246)
at org.apache.jsp.ws_005ftest_jsp._jspService(ws_005ftest_jsp.java:73)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:319)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:270)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:191)
at com.fireflow.other.CurrentUserFilter.doFilter(CurrentUserFilter.java:30)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:220)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:191)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:227)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:211)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:817)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:623)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:444)
at java.lang.Thread.run(Thread.java:595)
经查找未解决,于是把URL改成: Client client = new Client(new URL("http://localhost:8888/xx/services/test?wsdl"));因为服务类就是test,
改完后运行,再测试,又报异常,内容是:
java.lang.NoClassDefFoundError: org/apache/ws/commons/schema/resolver/URIResolver
at org.codehaus.xfire.client.Client.initFromDefinition(Client.java:262)
at org.codehaus.xfire.client.Client.<init>(Client.java:236)
at org.codehaus.xfire.client.Client.<init>(Client.java:246)
at org.apache.jsp.ws_005ftest_jsp._jspService(ws_005ftest_jsp.java:73)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:319)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:270)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:191)
at com.fireflow.other.CurrentUserFilter.doFilter(CurrentUserFilter.java:30)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:220)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:191)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:227)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:211)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:817)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:623)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:444)
at java.lang.Thread.run(Thread.java:595)
经查org/apache/ws/commons/schema/resolver/URIResolver在XmlSchema-1.1.jar中,于是加入该jar包,解决。这个包在xfire提供的包中有。