`
zxc3375
  • 浏览: 134604 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

cxf 实例解读

    博客分类:
  • java
cxf 
阅读更多

1.sample 实例之一---java_first_pojo

 服务端发布服务的方法:

HelloWorldImpl helloworldImpl = new HelloWorldImpl();
//cxf发布服务的工厂bean
ServerFactoryBean svrFactory = new ServerFactoryBean();
//设置服务类
svrFactory.setServiceClass(HelloWorld.class);
//设置服务地址
svrFactory.setAddress("http://localhost:9000/Hello");
//设置服务bean
svrFactory.setServiceBean(helloworldImpl);
svrFactory.create();
 

客户度调用的方法:

//创建服务代理工程bean
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
//设置服务代理地址
factory.setAddress("http://localhost:9000/Hello");
//创建代理服务
HelloWorld client = factory.create(HelloWorld.class);
//调用代理服务
System.out.println(client.sayHi(System.getProperty("user.name")));
 

2.sample实例之二---java_first_jaxws

服务端发布服务的方法:

 

HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld";
Endpoint.publish(address, implementor);

 

客户端调用的方法:

    private static final QName SERVICE_NAME  = new QName("http://server.hw.demo/", "HelloWorld");
    private static final QName PORT_NAME = new QName("http://server.hw.demo/", "HelloWorldPort");
        Service service = Service.create(SERVICE_NAME);
        // Endpoint Address
        String endpointAddress = "http://localhost:9000/helloWorld";
        // If web service deployed on Tomcat deployment, endpoint should be changed to:
        // String endpointAddress = "http://localhost:8080/java_first_jaxws/services/hello_world";

        // Add a port to the Service
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
        
        HelloWorld hw = service.getPort(HelloWorld.class);

 

 

3. sample实例之---java_first_jaxws_factory_bean

服务端发布服务的方法:

private static final QName SERVICE_NAME  = new QName("http://server.hw.demo/", "HelloWorld");
    private static final QName PORT_NAME = new QName("http://server.hw.demo/", "HelloWorldPort");
        Service service = Service.create(SERVICE_NAME);
        // Endpoint Address
        String endpointAddress = "http://localhost:9000/helloWorld";
        // If web service deployed on Tomcat deployment, endpoint should be changed to:
        // String endpointAddress = "http://localhost:8080/java_first_jaxws/services/hello_world";

        // Add a port to the Service
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
        
        HelloWorld hw = service.getPort(HelloWorld.class);

 

客户端调用的方法:

 

HelloWorldImpl implementor = new HelloWorldImpl();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:9000/helloWorld");
svrFactory.setServiceBean(implementor);
svrFactory.getInInterceptors().add(new LoggingInInterceptor());
svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
svrFactory.create();

 

4.sample实例之一---java_first_spring_support

服务端发布服务

/**
         * Important: This code simply starts up a servlet container and adds
         * the web application in src/webapp to it. Normally you would be using
         * Jetty or Tomcat and have the webapp packaged as a WAR. This is simply
         * as a convenience so you do not need to configure your servlet
         * container to see CXF in action!
         */
        org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server();

        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(9002);
        server.setConnectors(new Connector[] {connector});

        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();
 

客户度调用服务:

         ClassPathXmlApplicationContext context 
             = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
 
         HelloWorld client = (HelloWorld)context.getBean("client");
 
         String response = client.sayHi("Joe");
 

客户度调用小结

(引用http://blog.csdn.net/liaomin416100569/article/details/5503410)

  UserServiceImplService serivce = new UserServiceImplService();
  UserServiceImpl impl = serivce.getUserServiceImplPort();

 

   JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
   factory.setAddress("http://localhost:8088/abc");
   QName SERVICE = new QName("http://liaomin", "UserServiceImplService");
   factory.setServiceName(SERVICE);
   factory.setServiceClass(UserService.class);
   UserService us = (UserService) factory.create();
 
QName SERVICE = new QName("http://liaomin", "UserServiceImplService");
QName UserServiceImplPort = new QName("http://liaomin", "UserServiceImplPort");
URL url = new URL("http://localhost:8088/abc?wsdl");
ServiceDelegate dele=Provider.provider().createServiceDelegate(url,SERVICE,Service.class);
UserService us = (UserService) dele.getPort(UserServiceImplPort,UserService.class);
 
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(UserService.class);
factory.setAddress("http://localhost:8088/abc");
// factory.getServiceFactory().setDataBinding(new AegisDatabinding());
UserService client = (UserService) factory.create();
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics