Apache CXF Web 服务最简单的用法

之前一直用aix2来做服务,据说性能啥的不是很好,最近用了一下cxf发现很方便,下面来做个最简单的SOAP服务了解一下,当然cxf也可以用来做rest services等,只是一个注解的区别

 

创建Eclipse项目Maven项目

新建一个Maven项目

image

填写几个必要的参数,注意使用war包也就是web项目

image

配置Maven pom.xml的cxf jar包依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>fullstacks.cn</groupId>
	<artifactId>cxftest</artifactId>
	<version>1.0.0</version>

	<dependencies>


		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-databinding-aegis</artifactId>
			<version>3.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.1.2</version>
			<!-- <scope>test</scope> -->
		</dependency>

	</dependencies>
</project>

 

配置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_3_0.xsd" version="3.0">
  <display-name>cxfTest</display-name>

  
  <!-- cfx webSerivice -->  
    <servlet>    
        <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>    
    <session-config>    
      <session-timeout>60</session-timeout>    
    </session-config>  
</web-app>

 

服务端

接口

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
	@WebMethod	
	String sayHi(@WebParam(name="name") String name);  

}

接口的实现

import javax.jws.WebService;

@WebService(serviceName = "HelloWorlda", name = "HelloWorldb", portName = "HelloWorldcPort")
public class HelloWorldWs implements HelloWorld {

	@Override
	public String sayHi(String name) {
		return "Hello " + name + new Date();
	}

}

 

服务端的代码就这些,可以直接打包放到tomcat运行,或者在eclipse直接启动,访问地址为http://localhost:8080/cxftest/services?wsld

也可以使用cxf提供的jetty启动服务示例如下,下面这个运行之后的访问地址为http://127.0.0.1:8008/myws?wsld

public class ServerMain {
	 public static void main(String[] args) {  
	        HelloWorld hw = new HelloWorldWs();  
	        Endpoint.publish("http://127.0.0.1:8008/myws", hw);  
	        System.out.println("WebService已成功发布");  
	    }  
}

 

客户端

cxf提供访问服务有几种方式

1.Dynamic Client ,这是最简单的方法,没有之一

	 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(“http://127.0.0.1:8008/myws?wsld”); 
        Object[] res = client.invoke("sayHi","hl"); 
        System.out.println("Echo response: " + res[0]);

2.WSDL2Java generated Client , 这个需要用WSDL2Java命令生成所有需要的java类,然后调用

HelloService service = new HelloService();

Hello client = service.getHelloHttpPort();

String result = client.sayHi("Joe");

3.JAX-WS Proxy

import java.net.URL;

import javax.xml.ws.Service;

...

URL wsdlURL = new URL("http://localhost/hello?wsdl");

QName SERVICE_NAME = new QName("http://apache.org/hello_world_soap_http", "SOAPService");

Service service = Service.create(wsdlURL, SERVICE_NAME);

Greeter client = service.getPort(Greeter.class);

String result = client.greetMe("test");

4.JAX-WS Dispatch APIs , 这种方法比较原始,但是比较容易传送一些复杂的参数

import java.net.URL;

import javax.xml.transform.Source;

import javax.xml.ws.Dispatch;

import javax.xml.ws.Service;

...

URL wsdlURL = new URL("http://localhost/hello?wsdl");

Service service = Service.create(wsdlURL, new QName("HelloService"));

Dispatch<Source> disp = service.createDispatch(new QName("HelloPort"), Source.class, Service.Mode.PAYLOAD);

Source request = new StreamSource("<hello/>")

Source response = disp.invoke(request);

上面就是apache cxf服务端和客户端很简单的写法,基本上大部分的业务都可以满足了。不过项目依赖的jar包比较多,这是开源项目的通病,会用到海量的jar包,不过好在有这个强大Maven工具帮你解决这些问题。

如果不需要jetty进行调试,那么这个依赖项在pom.xml里面可以去掉,这样也是少一点jar包,后续有机会再研究一下spring提供的服务是如何使用的。