0.这个例子用了maven和spring,相关的内容不做赘述,只专注于cxf相关的内容。
1.首先搭建项目,这里我用的是maven webapp的工程,然后引入需要的jar包,pom文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tangjun.ws</groupId>
<artifactId>exmaple_cxf_server</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>exmaple_cxf_server Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>exmaple_cxf_server</finalName>
</build>
</project>
主要用到了cxf-rt-frontend-jaxws和cxf-rt-transprots-http两个jar包,依赖的jar包通过pom reader可以看见
2.下载完jar包后,配置web.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<!-- SPRING 配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXF配置 -->
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<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>
</web-app>
主要配了两个东西,一个是spring的监听,一个是CXF的servlet。servlet的url-pattern默认是list所有服务
3.编写服务的接口类package com.tangjun.ws.cxf.service.interfaces;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHi();
}
4.编写服务的实现类
package com.tangjun.ws.cxf.service.impl;
import java.util.Date;
import javax.jws.WebService;
import com.tangjun.ws.cxf.service.interfaces.HelloWorld;
@WebService(endpointInterface = "com.tangjun.ws.cxf.service.interfaces.HelloWorld")//这里填继承的接口
public class HelloWorldImpl implements HelloWorld {
public String sayHi() {
return "helloworld, "+new Date(System.currentTimeMillis());
}
}
5.在spring里配置服务
<?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">
<jaxws:endpoint id="helloWorld" implementor="com.tangjun.ws.cxf.service.impl.HelloWorldImpl" address="/helloWorld"/>
</beans>
注意比普通工程需要添加的内容是
xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
结果:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:sayHiResponse xmlns:ns2="http://interfaces.service.cxf.ws.tangjun.com/">
<return>helloworld, Wed Apr 22 13:58:07 CST 2015</return>
</ns2:sayHiResponse>
</soap:Body>
</soap:Envelope>