码迷,mamicode.com
首页 > Web开发 > 详细

Apache CXF实现WebService发布和调用

时间:2015-07-17 18:28:22      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

第一种方法:不用导入cxf jars

服务端:

1、 新建Web工程

2、新建接口和实现类、测试类

 目录结构图如技术分享下:

技术分享

接口代码:

package com.cxf.spring.service;

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

@WebService
public interface IGreetingService {

    @WebMethod
    public String greeting(String name);
    
    
}

实现类代码:

package com.cxf.spring.service;

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

@WebService
public class GreetingServiceImpl implements IGreetingService{

	@WebMethod
	@Override
	public String greeting(@WebParam(name ="name") String name) {

		return "HI:....."+name;
	}

}

测试类代码:

package com.cxf.spring.test;

import javax.xml.ws.Endpoint;

import com.cxf.spring.service.GreetingServiceImpl;

public class TestSpring {

    
    public static void main(String[] args) {
        pushWS_1();
    }
    
    
    public static void pushWS_1() {
        
        String address = "http://localhost:8100/greeting";
        
        System.out.println("Server start....");
        
        Endpoint.publish(address, new GreetingServiceImpl());
        
        System.out.println("Server ready....");
        
        try {
            Thread.sleep(3*60*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Server exit");
        System.exit(0);
    }
}

运行测试类:访问:http://localhost:8100/greeting?wsdl

技术分享

客户端:

 1、新建java工程  ,配置CXF环境变量 (下载Apache CXF2.7 )

 2、CMD打开命令窗口,运行以下命令,生产客户端代码: wsdl2java.bat  -p com.cxf.spting  -client -encoding utf-8 -noAddressBinding  http://localhost:8100/greeting?wsdl  

拷贝到新建java工程的src文件下

技术分享

技术分享

运行GreetingServiceImpl_GreetingServiceImplPort_Client.java访问webservice

技术分享

 

第二种:

新建web工程 引入cxf依赖包(最小jar)

技术分享

修改以上测试类代码

package com.cxf.spring.test;

import javax.xml.ws.Endpoint;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.cxf.spring.service.GreetingServiceImpl;
import com.cxf.spring.service.IGreetingService;

public class TestSpring {

    
    public static void main(String[] args) {
        pushWS_2();
    }
    
    public static void pushWS_2() {
    
        JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
        bean.setAddress("http://localhost:8100/greeting");
        bean.setServiceClass(IGreetingService.class);
        bean.setServiceBean(new GreetingServiceImpl());
        bean.create();
        
        System.out.println("Server ready....");
        
        try {
            Thread.sleep(3*60*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Server exit");
        System.exit(0);
    }

运行访问

客户端:按照第一种方法执行。。。

 

另外两种调用webservice的方法

新建工程 ------测试类   -----  接口:

package com.cxf.test;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import com.cxf.test.client.IGreetingService;

public class TestClient_2 {

    
    public static void main(String[] args) {  
           
        pull_1();
        
        pull_2();
    }
    
        
    public static void pull_1(){
         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
            Client client = dcf.createClient("http://localhost:8100/greeting?wsdl");  
            try {  
                Object[] objs = client.invoke("greeting", "张三");  
                System.out.println(objs[0].toString());  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
    }
    
    public static void pull_2() {
        
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();    
            factory.setServiceClass(IGreetingService.class);    
            factory.setAddress("http://localhost:8100/greeting?wsdl");    
            IGreetingService service = (IGreetingService) factory.create();    
            System.out.println(">>>>>>>>Client: " + service.greeting("战士"));
        
    }    
    
}
package com.cxf.test.client;

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

@WebService(targetNamespace="http://service.spring.cxf.com/", name = "IGreetingService")
public interface IGreetingService {

    @WebMethod
    public String greeting(String name);
    
    
}

 

Apache CXF实现WebService发布和调用

标签:

原文地址:http://www.cnblogs.com/liangblog/p/4655056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!