1.导入CXF相关的jar包和Spring相关的jar包,我建的是maven项目,就直接贴出maven依赖吧
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.7.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
2.web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXF相关配置 -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping>
</web-app>
3.接口
package cn.cjc.cxf;
import javax.jws.WebService;
@WebService
public interface IMessage {
String getMsg(String username);
}
4.实现类
package cn.cjc.cxf;
import javax.jws.WebService;
@WebService(endpointInterface = "cn.cjc.cxf.IMessage")
public class MessageImpl implements IMessage {
@Override
public String getMsg(String username) {
return username + ",欢迎学习CXF";
}
}
5.beans.xml文件配置
<!-- 在原有的Spring配置文件内引入CXF的jaxws命名空间 -->
<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">
<!-- 引入CXF默认配置文件 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<bean id="message" class="cn.cjc.cxf.MessageImpl"/>
<!-- 发布WS服务 -->
<jaxws:server address="/mymsg"><!-- 最终的服务发布地址将是http://localhost:8080/cxfspring/cxf/mymsg -->
<jaxws:serviceBean>
<ref bean="message"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
6.发布WebService服务,启动WEB服务器的同时即发布服务
7.调用服务,新建一个工程,第一步肯定是用wsimport或者wsdl2java命令生成客户端调用代码,接下来既可以用纯代码的方式调用,也可以将服务接口配置成受Spring管理的Bean,通过获取Bean来调用,下面调用方式为后者
a)引入Spring和CXF相关jar包,maven依赖同1
b)beans.xml文件配置
<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:client id="message" serviceClass="cn.cjc.cxf.IMessage" address="http://localhost:8080/cxfspring/cxf/mymsg"/>
</beans>
c)调用
package cn.cjc.cxf;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Invoke {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
IMessage service = context.getBean("message", IMessage.class);
System.out.println(service.getMsg("Junki"));
}
}