CXF入门实例

阅读更多

以下实现了一个CXF(apache-cxf-2.7.8)的入门实例HelloWorld,供大家参考。如有不足之处,请多多包涵。

1、需要导入的jar包

CXF入门实例_第1张图片
 2、工作空间的结构(MyEclipase8)

CXF入门实例_第2张图片
 

3、需要暴露的接口(Java代码)

 

package cxf.server;

import javax.jws.WebService;

/**
 * 服务接口
 * @author Administrator
 * 2014-1-17
 */
@WebService
public interface IHelloCXFService {
	
	/**
	 * 接受用户名,然后输出问好信息
	 * @param name
	 * @return
	 */
	public String sayHello(String name);
	
}

 

 

4、暴露接口的实现(Java代码)

 

package cxf.server;

import javax.jws.WebService;


/**
 * 服务的实现
 * @author Administrator
 * 2014-1-17
 */
@WebService(endpointInterface="cxf.server.IHelloCXFService", serviceName="helloCXF")
public class IHelloCXFServiceImpl implements IHelloCXFService {
	
	public IHelloCXFServiceImpl() {}

	public String sayHello(String name) {
		return "你好!" + name;
	}

}

 

 

5、CXF服务暴露配置文件(WebRoot/WEB-INF/cxf-servlet.xml)

 



  
	
	
		
                         
		  	
		
	
  

 

 

7、web.xml文件

 



	
  
    index.jsp
  
  
  
  
    CXFServlet
    org.apache.cxf.transport.servlet.CXFServlet
    1
  
  
    CXFServlet
    /services/*
  
  

 

 

8、启动服务,验证服务是否发布成功。服务地址格式:http://主机地址:端口/服务/Web服务。

http://localhost:8080/test_cxf/services 

services  实在web.xml中CXF的Servlet处配置的(/services/*)

发布服务成功界面:

CXF入门实例_第3张图片
 

9、使用Java代码调用Web服务,有一下三种方式:

package cxf.client;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import cxf.server.IHelloCXFService;

/**
 * CXF客户端调用
 * @author Administrator
 * 2014-1-20
 */
public class HelloCXFClient {
	/*
	 * 服务名称
	 * http://server.cxf/ —— 来自targetNamespace="http://server.cxf/"
	 * IHelloCXFService —— 暴露接口名称
	 */
	private static final QName SERVICE_NAME 
		= new QName("http://server.cxf/", "IHelloCXFService");
	/*
	 * 服务端口
	 * IHelloCXFServicePort —— 暴露接口名称 + Port
	 */
	private static final QName PORT_NAME 
		= new QName("http://server.cxf/", "IHelloCXFServicePort");
	
	
	public static void main(String[] args) throws Exception {
		// 1. 方式一
		// 服务端点地址:http://localhost:8080/test_cxf/services/helloCXF?wsdl 去掉 ?wsdl
		String endpointAddress = "http://localhost:8080/test_cxf/services/helloCXF";
		Service service = Service.create(SERVICE_NAME);
		service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
		IHelloCXFService hcxf = service.getPort(IHelloCXFService.class);
		System.out.println(hcxf.sayHello("tom"));
		
		// 2. 方式二
		// 根据wsdl文件动态调用WebService
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		org.apache.cxf.endpoint.Client client = dcf
				.createClient("http://localhost:8080/test_cxf/services/helloCXF?wsdl");
		// sayHello 为接口中定义的方法名称 张三为传递的参数 返回一个Object数组
		Object[] objects = client.invoke("sayHello", new Object[]{"Bear"});
		// 输出调用结果
		System.out.println(objects[0].toString());
		
		// 3. 方式三
		JaxWsProxyFactoryBean  factoryBean=new JaxWsProxyFactoryBean();
        factoryBean.getInInterceptors().add(new LoggingInInterceptor());
        factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
        factoryBean.setServiceClass(IHelloCXFService.class);
        factoryBean.setAddress("http://localhost:8080/test_cxf/services/helloCXF");
        IHelloCXFService impl=(IHelloCXFService) factoryBean.create();
        System.out.println(impl.sayHello("Bluse"));
	}
	
}

 

10、调用结果

2014-1-20 10:16:51 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass

信息: Creating Service {http://server.cxf/}IHelloCXFService from class cxf.server.IHelloCXFService

2014-1-20 10:16:52 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass

信息: Creating Service {http://server.cxf/}IHelloCXFService from class cxf.server.IHelloCXFService

你好!tom

2014-1-20 10:16:53 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames

信息: Created classes: cxf.server.ObjectFactory, cxf.server.SayHello, cxf.server.SayHelloResponse

你好!Bear

2014-1-20 10:16:53 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass

信息: Creating Service {http://server.cxf/}IHelloCXFServiceService from class cxf.server.IHelloCXFService

2014-1-20 10:16:53 org.apache.cxf.services.IHelloCXFServiceService.IHelloCXFServicePort.IHelloCXFService

信息: Outbound Message

---------------------------

ID: 1

Address: http://localhost:8080/test_cxf/services/helloCXF

Encoding: UTF-8

Http-Method: POST

Content-Type: text/xml

Headers: {Accept=[*/*], SOAPAction=[""]}

Payload: Bluse

--------------------------------------

2014-1-20 10:16:53 org.apache.cxf.services.IHelloCXFServiceService.IHelloCXFServicePort.IHelloCXFService

信息: Inbound Message

----------------------------

ID: 1

Response-Code: 200

Encoding: UTF-8

Content-Type: text/xml;charset=UTF-8

Headers: {Content-Length=[216], content-type=[text/xml;charset=UTF-8], Date=[Mon, 20 Jan 2014 02:16:53 GMT], Server=[Apache-Coyote/1.1]}

Payload: 你好!Bluse

--------------------------------------

你好!Bluse

 

 

项目源码:Test_CXF.zip 压缩文件

 

更多IT技术请浏览:www.hxstrive.com

 

 

 

 

  • CXF入门实例_第4张图片
  • 大小: 14.8 KB
  • CXF入门实例_第5张图片
  • 大小: 12.2 KB
  • CXF入门实例_第6张图片
  • 大小: 8.9 KB
  • Test_CXF.zip (9.6 MB)
  • 下载次数: 6
  • 查看图片附件

你可能感兴趣的:(CXF入门实例)