cxf开发webservice服务端

一.  简介
Apache CXF 是一个Service框架,他简化了Service的创建, CXF实现了JAX-WS2.0规范,并通过了JAX-WS2.0 TCK; CXF和Spring无缝集成;CXF支持多种传输协议(HTTP, JMS, Corba等), 支持多种Binding数据格式(SOAP,XML,JSON等), 支持多种DataBinding数据类型(JAXB, Aegis) 。CXF基于Interceptor的架构,使得整个框架非常易于扩展。


在项目中经常会使用cxf开发webservice,所以写一些相关的文章,留下一些笔记。

本文将介绍的是通过maven来实现cxf开发。


首先编写maven中的pom.xml配置cxf所依赖的jar包,内容如下:





1.在web.xml文件中编写cxf所对应的servlet

<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
<servlet-mapping>



2.声明接口,事项相关的接口。
@WebService
public interface IHelloWorld {

void sayHello(String str);
}


@WebService
public class HelloWorldImpl implements IHelloWorld {

public void sayHello(String str) {
System.out.println("say " + str);
}
}

3.编写cxf文件,发布接口

<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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="helloWorld" implementor="com.liuwuhen.cxf.HelloWorldImpl"
address="/sayHello">
</jaxws:endpoint>
</beans>

4.在web.xml中添加加载spring配置信息。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/cxf.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


5.执行mvn clean install -Ptomcat 运行,项目访问http://localhost:8080/cxf-test/services/sayHello?wsdl 即可。

猜你喜欢

转载自liuwuhen.iteye.com/blog/1666189