梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
demo用的是cxf 3.0
接口:
 1package com;
 2
 3import javax.jws.WebParam;
 4import javax.jws.WebService;
 5
 6@WebService
 7public interface HelloWorldServiceInf
 8{
 9    public String sayHello(@WebParam(name = "username")
10    String username);
11}


2. 实现类
package com;

import javax.jws.WebService;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;


@WebService(endpointInterface
="com.HelloWorldServiceInf",serviceName="helloWorldService")
public class Server implements HelloWorldServiceInf {

    
public String sayHello(String username) {
        
return "Hello,"+username;
    }


    
    
public static void main(String[] args) {
        Server impl
=new Server();
        JaxWsServerFactoryBean factoryBean
=new JaxWsServerFactoryBean();
        factoryBean.setAddress(
"http://localhost:9000/hello");
        factoryBean.setServiceClass(HelloWorldServiceInf.
class);
        factoryBean.setServiceBean(impl);
        factoryBean.getInInterceptors().add(
new LoggingInInterceptor());
        factoryBean.getOutInterceptors().add(
new LoggingOutInterceptor());
        factoryBean.create();
    }

    
}



3. 客户端调用
 1import org.apache.cxf.endpoint.Client;
 2import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 3
 4public class ClientX
 5{   
 6    public static void main(String[] args)throws Exception 
 7    {
 8        String wsdlUrl = "http://localhost:9000/hello?wsdl";
 9        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
10        Client client= factory.createClient(wsdlUrl);
11        Object[] res = client.invoke("sayHello""hwp");
12        System.out.println(res[0]);
13    }

14
15}

package com.cxf.client;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.cxf.interfaces.HelloWorldServiceInf;

public class Client {
    
public static void main(String[] args) {
        JaxWsProxyFactoryBean  factoryBean
=new JaxWsProxyFactoryBean();
        factoryBean.getInInterceptors().add(
new LoggingInInterceptor());
        factoryBean.getOutInterceptors().add(
new LoggingOutInterceptor());
        factoryBean.setServiceClass(HelloWorldServiceInf.
class);
        factoryBean.setAddress(
"http://localhost:9000/hello");
        HelloWorldServiceInf impl
=(HelloWorldServiceInf) factoryBean.create();
        System.out.println(impl.sayHello(
"张三"));
    }

}



posted on 2014-07-07 21:42 HUIKK 阅读(315) 评论(0)  编辑  收藏 所属分类: webservice

只有注册用户登录后才能发表评论。


网站导航: