一、下载jar包
jar包地址:http://cxf.apache.org/download.html
二、建立一个WebProject
导入相关jar包(apache-cxf-2.7.5.zip)
三、建立服务端
1、新建一个WS接口
@WebService
public interface HelloWorld {
public String getName(String name);
}
2、新建WS接口 实现类
package com.cxf.server.impl;
import javax.jws.WebService;
import com.cxf.server.HelloWorld;
@WebService(endpointInterface = "com.cxf.server.HelloWorld", serviceName = "helloWorldImpl")
public class HelloWorldImpl implements HelloWorld {
@Override
public String getName(String name) {
System.out.println("Server-------"+name);
return name;
}
}
注解说明:
*@WebService:申明为webservice的注解,这样cxf就知道这是个webservice了
*endpointInterface:要暴露的接口类
*serviceName : 服务名,其实就是调用地址
3、在WEB-INF目录下新建cxf-webservice.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 Apache CXF Bean Definition --> <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="helloWorldImpl" implementor="com.cxf.server.impl.HelloWorldImpl" address="/helloWorld"> </jaxws:endpoint> </beans>
xml说明:
implementor :接口类的实现类
address: 要和注释里面声明的服务名对应,对应的是外部访问地址
4、在修改web.xml文件,如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/cxf-webservice.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>/*</url-pattern> </servlet-mapping> </web-app>
5、将项目发布到tomcat,启动tomcat
6、浏览器输入http://localhost:8080/TestWeb/helloWorld?wsdl 输出相应结果说明服务端创建成功。
四、编写调用WS测试代码
package com.cxf.client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.cxf.server.HelloWorld;
public class TestHelloWorld {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:8080/TestWeb/helloWorld");
HelloWorld client = (HelloWorld) factory.create();
String reply = client.getName("Test01");
System.out.println(reply);
}
}