cxf 3.0.3 与spring 3结合初探

来源:互联网 发布:aoi编程手册 编辑:程序博客网 时间:2024/05/17 22:35

    近日发现有些服务只以打包的形式向他人提供显得有些不科学、不合理,于是便研究一下以webservice的方式提供服务。而webservice通常有两种开源方式,一种是axis,另一种是cxf。由于需要与spring结合,所以采用了cxf的方式。


    服务端

    接口类
    
import javax.jws.WebService;@WebServicepublic interface IHelloService {String sayHi(String text);}
接口的实现类
import java.io.IOException;import javax.jws.WebService;@WebService(endpointInterface="com.IHelloService")public class HelloServiceImpl implements IHelloService {@Overridepublic String sayHi(String text) {// TODO Auto-generated method stubreturn "Hello " + text;}}
与spring结合的话需要配置web.xml与cxf-servlet.xml
首先是web.xml
<servlet>        <servlet-name>cxf</servlet-name>        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>
<servlet-mapping>        <servlet-name>cxf</servlet-name>        <url-pattern>/services/*</url-pattern>    </servlet-mapping>

然后是cxf-servlet.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">    <import resource="classpath:META-INF/cxf/cxf.xml"/>    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>    <jaxws:endpoint id="helloWorld" implementor="com.HelloServiceImpl" address="/HelloWorld"/></beans>


将项目发布到tomcat中,启动,在浏览器中输入http://localhost:8080/项目名称/services/HelloWorld?wsdl,之后浏览器会呈现出xml信息,这时说明你的webservice配置成功了!

最后附上用到的cxf jar包
cxf-core-3.0.3.jar
cxf-rt-bindings-soap-3.0.3.jar
cxf-rt-frontend-jaxws-3.0.3.jar
cxf-rt-frontend-simple-3.0.3.jar
cxf-rt-wsdl-3.0.3.jar
jaxb-api-2.2.11.jar
stax2-api-3.1.4.jar
woodstox-core-asl-4.4.1.jar
wsdl4j-1.6.3.jar
xmlschema-core-2.1.0.jar
cxf-rt-databinding-jaxb-3.0.3.jar


0 0
原创粉丝点击