工具:IDEA 14
步骤一 新建项目
项目名CXFWebService
新建两个模块webService_server和webService_client
步骤二 引入依赖
<modules>
<module>WebService_server</module>
<module>WebService_Client</module>
</modules>
<groupId>me.yulin.rao</groupId>
<artifactId>CXFWebServices</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<cxf.version>2.7.5</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
</dependencies>
步骤四 创建服务端
编写暴露在外的服务接口
import javax.jws.WebService;
@WebService
public interface IhelloWorld {
public String sayHi(String who);
}
编写接口的实现
import com.yoyo.Interface.IhelloWorld;
import javax.jws.WebService;
@WebService(endpointInterface = "com.yoyo.Interface.IhelloWorld")
public class HelloWorldImpl implements IhelloWorld {
@Override
public String sayHi(String who) {
return "Hi,"+who;
}
}
服务端用jetty起,所以先配置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"
id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<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>
<init-param>
<param-name>config-location</param-name>
<param-value>WEB-INF/cxf-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
接下来就是配置cxf的servlet:在WEB-INF路径下新建cxf-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:simple="http://cxf.apache.org/simple"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">
<simple:server id="helloWorld" serviceClass="com.yoyo.Interface.IhelloWorld" address="http://localhost:8080/Interface/IhelloWorld?wsdl">
<simple:serviceBean>
<bean class="com.yoyo.Impl.HelloWorldImpl" />
</simple:serviceBean>
</simple:server>
</beans>
说明一下,这里的address可以自定义,如果在本地跑的话,只需要和后面客户端的cxf-client.xml中的address一致即可,但是server端的address末尾需要加上“?wsdl”。
并且在这里使用的是不用任何注解,完全依赖反射实现WS的编程模型”simple”,还有另外三种模型:Annotations,Dynamic Clients,JAX-WS。
另外,不要忘了webService_server模块自己的pom文件
步骤五 创建客户端
客户端主要是Spring配置,代码量较少
首先是cxf-client.xml配置(xml的名字随意)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:simple="http://cxf.apache.org/simple"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">
<simple:client id="helloWorld"
serviceClass="com.yoyo.Interface.IhelloWorld" address="http://localhost:8080/Interface/IhelloWorld" />
</beans>
配置好之后,新增一个SpringClient类,测试一下能否调用服务端的服务
import com.yoyo.Interface.IhelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringClient {
public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext("cxf-client.xml");
IhelloWorld client =(IhelloWorld)context.getBean("helloWorld");
System.out.println(client.sayHi("yoyo"));
}
}
步骤六 配置好jetty测试一下
将jetty跑起来后,浏览器显示如下:
然后run我们刚编写的SpringClient
控制台显示如下:
到此demo结束