说明 (来源于百度百科)
Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。
添加jar包支持
以下是pom文件(直接给出dependencies节点)
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
</dependency>
</dependencies>
服务端
配置说明
- 配置spring整合CXF提供webService服务
- 采用jetty启动服务暴露在指定端口
- 创建需要暴露服务的接口并实现
操作如下
服务端包如下:
HelloWorld.java
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
HelloWorldImpl.java
import javax.jws.WebService;
@WebService(endpointInterface = "demo.spring.service.HelloWorld")//标记提供服务的接口
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
}
Server.java
public class Server {
protected Server() throws Exception {
System.out.println("Starting Server");
org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(9002);
WebAppContext webappcontext = new WebAppContext();
webappcontext.setContextPath("/");
webappcontext.setWar("target/JavaFirstSpringSupport.war");
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] {webappcontext, new DefaultHandler()});
server.setHandler(handlers);
server.start();
System.out.println("Server ready...");
server.join();
}
//启动jetty服务器
public static void main(String args[]) throws Exception {
new Server();
}
}
服务端配置文件server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<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="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/>
</beans>
并且在web.xml配置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>
运行服务端等待
测试是否成功 http://ip:host/services
运行成功能看到一个服务的列表
客户端
配置说明
- 需要知道服务端具体暴露的地址和具体的接口
- 配置CXF工程调用服务,并且使用CXF工厂的create进行生产具体的代理实现类
注意
- 服务端和调用端的接口包名要一致
- 在客户端和服务端的接口注解上指定相同的targetNamespace。建议与包名保持一致。
操作如下
客户端包如下:
HelloWorld.java (接口与服务端一致)
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
TestHelloworldClient.java (测试类)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:webservice-client.xml"})
public class TestHelloworldClient {
@Autowired
private HelloWorld helloWorld; //注入
@Test
public void sayHello()throws Exception {
String response = helloWorld.sayHi("Joe");
System.out.println("Response: " + response);
System.exit(0);
}
}
webservice-client.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/schema/jaxws.xsd">
<!--注意包名必须一致 -->
<bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.service.HelloWorld"/>
<!--注意地址能否访问 -->
<property name="address" value="http://localhost:9002/services/HelloWorld"/>
</bean>
</beans>