导入相关的jar文件;
CXFSpring服务器端:
1.(定义接口)新建一个服务接口IHelloWorld.java,该接口是面向客户端,暴露服务:- package cn.com.service;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface IHelloWorld {
- //@WebParam(name="arg0")可有可无,为了增强可读性
- public String sayHello(@WebParam(name="arg0")String text);
- }
2.(实现接口)再新建该接口的实现类HelloWorldImpl.java:
- package cn.com.service;
- import javax.jws.WebService;
- @WebService(endpointInterface="cn.com.service.IHelloWorld")
- public class HelloWorldImpl implements IHelloWorld {
- public String sayHello(String text) {
- return "Hello" + text ;
- }
- }
3.现在可以进行spring配置了,applicationContext.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:p="http://www.springframework.org/schema/p"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xmlns:cxf="http://cxf.apache.org/core"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.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" />
- <bean id="hello" class="cn.com.service.HelloWorldImpl"/>
- <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
- </beans>
注意:1).<beans>元素里面的命名空间一定要正确,特别要增加xmlns:jaxws="http://cxf.apache.org/jaxws",http://cxf.apache.org/jaxws,http://cxf.apache.org/schemas/jaxws.xsd;
2).cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml三个文件都在cxf-2.0.7.jar中把它们拷贝到META-INF/目录下。
4.配置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:applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <display-name>CXF Servlet</display-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>/*</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
到此服务器端基本上告一段落,可以将应用部署到tomcat,启动并访问http://localhost:8080/webws/HelloWorld?wsdl,如果能正确显示xml文件则说明部署成功。
CXFSpring客户端:
1.(定义接口)新建一个IHelloWorld接口,这是服务器端暴露的服务,代码和服务器端一致即可
2.新建客户程序,调用服务器端服务:
1)在页面中注入客户端实例
@Resource(name= "IHelloWorld")
private IHelloWorld iHelloWorld;
2)在方法中使用
String result = iHelloWorld.sayHello("你好!");System.out.println(result);
3.配置客户端的spring文件
id="client"
/>
注意:address="http://localhost:8080/webws/HelloWorld" 中的/HelloWorld要与服务器端applicationContext.xml中的<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />的address属性对应。
现在服务器和客户端都已完成,启动tomcat,运行客户程序,将输出结果:
Hello你好!