xfire集成spring开发webservice怎么配置发布地址
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xfire集成spring开发webservice怎么配置发布地址相关的知识,希望对你有一定的参考价值。
1.首先建一个Web工程,引入相应的jar包,Xfire开发最精简jar包下载服务器端:
commons-logging-1.1.1.jar
jdom-1.0.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
客户端:
com.springsource.org.junit-4.7.0.jar
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.1.1.jar
jdom-1.0.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
XmlSchema-1.1.jar
2.修改web.xml,加入以下代码:
[html] view plain copy
<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>xfireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
3.在classpath下加入Spring配置文件applicationContext.xml,加入以下代码:
[html] view plain copy
<context:component-scan base-package="my.webservice" />
<!-- XFire start -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />
<bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
<property name="xfire" ref="xfire" />
<property name="webAnnotations" ref="webAnnotations" />
</bean>
<!-- XFire end -->
4.定义WebService接口,添加相应注解:
[java] view plain copy
@WebService
public interface IBookService
@WebMethod
public Book getBook();
5.接口实现类,注解中serviceName定义发布的服务名,endpointInterface定义实现的接口:
[java] view plain copy
@Component
@WebService(serviceName="BookService",
endpointInterface = "my.webservice.IBookService")
public class BookServiceImpl implements IBookService
@Override
public Book getBook()
Book b = new Book(1, "Java核心思想", 100);
System.out.println(">>>>>>Server: " + b);
return b;
6.以上便是服务端的配置及实现,是不是很简单。把工程发布到Tomcat并启动,在浏览器中输入:http://127.0.0.1:8080/XFireTest/service/BookService?wsdl(BookService为上面serviceName定义的名称),如果浏览器中显示BookService相关xml信息,则表示WebService发布成功。
7.客户端调用服务端方法:
(1)使用接口调用,此方法需要发布服务者提供接口,或我们自己通过wsdl生成接口。自己生成接口方法:
wsimport -keep -p my.client http://127.0.0.1:8080/XFireTest/service/BookService?wsdl
-keep 指示保留生成的文件,-p 指定需要在其中生成构件的包名称。http://127.0.0.1:8080/XFireTest/service/BookService?wsdl 是WSDL文件的位置。
wsimport在JAVA_HOME/bin目录下,应该已加到path环境变量中,可直接在cmd中运行以上命令,注意先进入要生成类的目录(如src)再执行。
[java] view plain copy
@Test
public void testBookService()
Service serviceModel = new ObjectServiceFactory().create(IBookService.class);
String url = "http://127.0.0.1:8080/XFireTest/service/BookService";
IBookService service = null;
try
service = (IBookService) new XFireProxyFactory().create(serviceModel, url);
Book b = service.getBook();
System.out.println(">>>>>>>>Client: " + b);
catch (Exception e)
e.printStackTrace();
(2)通过wsdl调用,该方法如果返回值是String可正常使用,如果像本列中返回Book类型,则打印值为[#document: null],返回类型是org.apache.xerces.dom.DocumentImpl,需手动解析。我在项目中使用时,发布服务的时候把数据用xml形式返回,客户端用dom4j解析。
[java] view plain copy
@Test
public void wsdlTest() throws MalformedURLException, Exception
Client client = new Client(new URL("http://127.0.0.1:8080/XFireTest/service/BookService?wsdl"));
Object[] results = client.invoke("getBook", new Object[] );
System.out.println(results[0]);
参考技术A
给你个简单的例子吧
<?xml version="1.0" encoding="UTF-8"?><beans
xmlns="
xmlns:xsi="
xmlns:jaxws="
xsi:schemaLocation="
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!-- jaxws:server示例 首先在web.xml中添加servlet
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/deviceWService/*</url-pattern>
</servlet-mapping>
<jaxws:server id="androidWService" serviceClass="webservice注解的服务类" address="/访问路径">
<jaxws:serviceBean/>
</jaxws:server>
-->
<jaxws:server id="androidWService" serviceClass="com.my.project.plugin.webservice.MyWebservice" address="/ddd">
</jaxws:server>
</beans>
@WebService
public class MyWebservice
public String ddd()
return "MyWebservice";
JDK1.7+Tomcat7+Eclipse+XFire开发WebService
XFire是新一代的Java Web服务引擎,XFire使得在JavaEE应用中发布Web服务变得轻而易举。和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地和Spring集成,它使得Java开发人员终于可以获得和.Net开发人员一样的开发效率。官方网站:http://xfire.codehaus.org/
1.XfFireDemo目录结构
涉及jar:
/XFireDemo/WebContent/WEB-INF/lib/commons-codec-1.2.jar
/XFireDemo/WebContent/WEB-INF/lib/commons-httpclient-3.1.jar
/XFireDemo/WebContent/WEB-INF/lib/commons-logging-1.0.4.jar
/XFireDemo/WebContent/WEB-INF/lib/jdom-1.0.jar
/XFireDemo/WebContent/WEB-INF/lib/spring-1.2.6.jar
/XFireDemo/WebContent/WEB-INF/lib/stax-api-1.0.1.jar
/XFireDemo/WebContent/WEB-INF/lib/wsdl4j-1.6.1.jar
/XFireDemo/WebContent/WEB-INF/lib/xbean-spring-2.8.jar
/XFireDemo/WebContent/WEB-INF/lib/xfire-all-1.2.6.jar
/XFireDemo/WebContent/WEB-INF/lib/XmlSchema-1.4.6.jar
/XFireDemo/WebContent/WEB-INF/lib/xstream-1.3.1.jar
2.services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<service xmlns="http://xfire.codehaus.org/config/1.0">
<!-- 服务名称,客户端调用时需要这个 -->
<name>XFireDemo</name>
<!-- 一般是网址,意义不大 -->
<namespace>http://www.hello.com</namespace>
<!-- 接口类 -->
<serviceClass>service.IHelloWorld</serviceClass>
<!-- 实现类 -->
<implementationClass>service.HelloWorld</implementationClass>
</service>
</beans>
3.修改WebContentWEB-INFweb.xml配置文件的内容,下面是修改后web.xml:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>XFireDemo</display-name>
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
4.在浏览器中输入http://localhost:8080/XFireDemo/services会得到正确的web服务已经成功部署
5.附上Demo源码百度云:https://pan.baidu.com/s/1P_WUlxR4InnehSNquR04BA 提取码:63dj
以上是关于xfire集成spring开发webservice怎么配置发布地址的主要内容,如果未能解决你的问题,请参考以下文章