zoukankan      html  css  js  c++  java
  • 使用CXF发布和调用webservice之HelloWorld入门

    依赖的JAR
        cxf-2.2.10.jar
        jetty-6.1.21.jar
        jetty-util-6.1.21.jar
        servlet-2_5-api.jar
        wsdl4j-1.6.2.jar
        XmlSchema-1.4.5.jar
    创建一个普通的Java工程即可

    创建webservice接口

    package com.cxf.interfaces;
    
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    @WebService
    public interface HelloWorldServiceInf {
        
        String sayHello(@WebParam(name="username") String username);
        
    }

    发布和调用webservice
            方法一
    发布webservice

    package com.cxf.impl;
    
    import javax.jws.WebService;
    
    import org.apache.cxf.interceptor.LoggingInInterceptor;
    import org.apache.cxf.interceptor.LoggingOutInterceptor;
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    
    import com.cxf.interfaces.HelloWorldServiceInf;
    
    @WebService(endpointInterface="com.cxf.interfaces.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();
        }
        
    }

    wsdl描述文件

     <?xml version="1.0" ?> 
    - <wsdl:definitions name="HelloWorldServiceInfService" targetNamespace="http://interfaces.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://interfaces.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    - <wsdl:types>
    - <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://interfaces.cxf.com/" xmlns:tns="http://interfaces.cxf.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="sayHello" type="tns:sayHello" /> 
    - <xsd:complexType name="sayHello">
    - <xsd:sequence>
      <xsd:element minOccurs="0" name="username" type="xsd:string" /> 
      </xsd:sequence>
      </xsd:complexType>
      <xsd:element name="sayHelloResponse" type="tns:sayHelloResponse" /> 
    - <xsd:complexType name="sayHelloResponse">
    - <xsd:sequence>
      <xsd:element minOccurs="0" name="return" type="xsd:string" /> 
      </xsd:sequence>
      </xsd:complexType>
      </xsd:schema>
      </wsdl:types>
    - <wsdl:message name="sayHelloResponse">
      <wsdl:part element="tns:sayHelloResponse" name="parameters" /> 
      </wsdl:message>
    - <wsdl:message name="sayHello">
      <wsdl:part element="tns:sayHello" name="parameters" /> 
      </wsdl:message>
    - <wsdl:portType name="HelloWorldServiceInf">
    - <wsdl:operation name="sayHello">
      <wsdl:input message="tns:sayHello" name="sayHello" /> 
      <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /> 
      </wsdl:operation>
      </wsdl:portType>
    - <wsdl:binding name="HelloWorldServiceInfServiceSoapBinding" type="tns:HelloWorldServiceInf">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
    - <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document" /> 
    - <wsdl:input name="sayHello">
      <soap:body use="literal" /> 
      </wsdl:input>
    - <wsdl:output name="sayHelloResponse">
      <soap:body use="literal" /> 
      </wsdl:output>
      </wsdl:operation>
      </wsdl:binding>
    - <wsdl:service name="HelloWorldServiceInfService">
    - <wsdl:port binding="tns:HelloWorldServiceInfServiceSoapBinding" name="HelloWorldServiceInfPort">
      <soap:address location="http://localhost:9000/hello" /> 
      </wsdl:port>
      </wsdl:service>
      </wsdl:definitions>

    客户端调用

    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("张三"));
        }
    }

     方法二
    发布webservice

    package com.cxf.impl;
    
    import javax.jws.WebService;
    import javax.xml.ws.Endpoint;
    
    import com.cxf.interfaces.HelloWorldServiceInf;
    
    @WebService(endpointInterface="com.cxf.interfaces.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();
            String address="http://localhost:9000/hello";
            Endpoint.publish(address, impl);
        }
    }

    wsdl文件

     <?xml version="1.0" ?> 
    - <wsdl:definitions name="helloWorldService" targetNamespace="http://impl.cxf.com/" xmlns:ns1="http://interfaces.cxf.com/" xmlns:ns2="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" /> 
    - <wsdl:binding name="helloWorldServiceSoapBinding" type="ns1:HelloWorldServiceInf">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
    - <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document" /> 
    - <wsdl:input name="sayHello">
      <soap:body use="literal" /> 
      </wsdl:input>
    - <wsdl:output name="sayHelloResponse">
      <soap:body use="literal" /> 
      </wsdl:output>
      </wsdl:operation>
      </wsdl:binding>
    - <wsdl:service name="helloWorldService">
    - <wsdl:port binding="tns:helloWorldServiceSoapBinding" name="ServerPort">
      <soap:address location="http://localhost:9000/hello" /> 
      </wsdl:port>
      </wsdl:service>
      </wsdl:definitions>

    客户端调用

    package com.cxf.client;
    
    import javax.xml.namespace.QName;
    import javax.xml.ws.Service;
    import javax.xml.ws.soap.SOAPBinding;
    
    import com.cxf.interfaces.HelloWorldServiceInf;
    
    public class Client {
        //注意:此处http://interfaces.cxf.com/  来源于wsdl文件中namespace   <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" /> 
    
        private static final QName SERVICE_NAME=new QName("http://interfaces.cxf.com/","HelloWorldServiceInf");//HelloWorldServiceInf接口类的名称
        private static final QName PORT_NAME=new QName("http://interfaces.cxf.com/", "HelloWorldServiceInfPort");//HelloWorldServiceInfPort 接口类的名称+Port
        public static void main(String[] args) {
            String endPointAddress="http://localhost:9000/hello";
            Service service=Service.create(SERVICE_NAME);
            service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
            HelloWorldServiceInf inf=service.getPort(HelloWorldServiceInf.class);
            System.out.println(inf.sayHello("张三"));
        }
    }

    CXF根据wsdl文件动态调用WebService

    package com.cxf.client;
    
    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
    
    public class ClientFromWsdl {
        
        public static void main(String[] args) throws Exception{
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9000/hello?wsdl");
            //sayHello 为接口中定义的方法名称   张三为传递的参数   返回一个Object数组
            Object[] objects=client.invoke("sayHello", "张三"); 
            //输出调用结果
            System.out.println(objects[0].toString());
        }
    }

    参考原文:http://www.blogjava.net/sxyx2008/archive/2010/09/15/332058.html

  • 相关阅读:
    [转载]我的PMP复习备考经验谈(下篇)——一本关于PMP备考的小指南
    安装MongoDB遇到问题
    安装MongoDB遇到问题
    (热死你)Resin https ssl Linux 配置,实战可用
    高性能web服务器(热死你)Resin Linux的安装、配置、部署,性能远超Nginx支持Java、PHP等
    我最近用Python写了一个算法,不需要写任何规则就能自动识别一个网页的内容
    20161230实时量化监控,成效显著,实在忍不住要给大家秀一把
    16年收官之战,堪称完美,祝愿大家2017一举成名天下闻,虎啸龙吟展宏图
    我3年前开发的IM即时通讯一直没勇气推出,现在智能时代了,有什么可以结合的地方吗?
    忙活了一周时间,开发了一个年会抽奖系统,免费开放给大家(含操作视频及下载地址)
  • 原文地址:https://www.cnblogs.com/duffy/p/5133003.html
Copyright © 2011-2022 走看看