Creating a SOAP client in java given a WSDL file

前端 未结 1 1678
隐瞒了意图╮
隐瞒了意图╮ 2021-02-02 14:09

I have never used any SOAP libraries in java, so I apologize for the newby-ness of this question.

I am trying to take advantage of Tivoli Endpoint Manager\'s SOAP API. I

1条回答
  •  温柔的废话
    2021-02-02 14:33

    There is a maven plugin, the cxf-codegen-plugin, that will auto generate java classes from a WSDL. It will also create a stub client for you if you want to see how to use the generated code.


    Edit 2019-11-15

    For this example I downloaded the wsdl located here* and saved it as stockquote.wsdl.

    * The wsdl is no longer available from where I initially downloaded it. Find the wsdl included in this answer further below.


    $ tree

    .
    |-- cxf-wsdl2java-example.iml
    |-- pom.xml
    `-- src
        |-- main
        |   |-- java
        |   |   `-- com
        |   |       `-- stackoverflow
        |   |           `-- App.java
        |   `-- resources
        |       `-- stockquote.wsdl
        `-- test
            `-- java
                `-- com
                    `-- stackoverflow
                        `-- AppTest.java
    
    10 directories, 5 files
    

    $ cat pom.xml

    
      4.0.0
    
      com.stackoverflow
      cxf-wsdl2java-example
      1.0
      jar
    
      cxf-wsdl2java-example
      http://maven.apache.org
    
      
        UTF-8
      
    
      
        
          junit
          junit
          4.11
          test
        
      
    
      
        
          
            org.apache.cxf
            cxf-codegen-plugin
            2.7.7
            
              
                generate-sources
                generate-sources
                
                  ${project.build.directory}/generated/cxf
                  
                    
                      ${basedir}/src/main/resources/stockquote.wsdl
                      
                        -impl
                        -verbose
                      
                    
                  
                
                
                  wsdl2java
                
              
            
          
        
      
    
    
    

    $ mvn generate-sources

    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building cxf-wsdl2java-example 1.0
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- cxf-codegen-plugin:2.7.7:wsdl2java (generate-sources) @ cxf-wsdl2java-example ---
    Loading FrontEnd jaxws ...
    Loading DataBinding jaxb ...
    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.462s
    [INFO] Finished at: Mon Oct 21 14:44:54 MDT 2013
    [INFO] Final Memory: 11M/211M
    [INFO] ------------------------------------------------------------------------
    

    $ tree .

    .
    |-- cxf-wsdl2java-example.iml
    |-- pom.xml
    |-- src
    |   |-- main
    |   |   |-- java
    |   |   |   `-- com
    |   |   |       `-- stackoverflow
    |   |   |           `-- App.java
    |   |   `-- resources
    |   |       `-- stockquote.wsdl
    |   `-- test
    |       `-- java
    |           `-- com
    |               `-- stackoverflow
    |                   `-- AppTest.java
    `-- target
        |-- classes
        |-- cxf-codegen-plugin-markers
        `-- generated
            `-- cxf
                `-- net
                    `-- webservicex
                        |-- GetQuote.java
                        |-- GetQuoteResponse.java
                        |-- ObjectFactory.java
                        |-- package-info.java
                        |-- StockQuoteHttpGetImpl.java
                        |-- StockQuoteHttpGet.java
                        |-- StockQuoteHttpPostImpl.java
                        |-- StockQuoteHttpPost.java
                        |-- StockQuote.java
                        |-- StockQuoteSoapImpl1.java
                        |-- StockQuoteSoapImpl.java
                        `-- StockQuoteSoap.java
    
    17 directories, 17 files
    

    $ cat target/generated/cxf/net/webservicex/StockQuote.java

    package net.webservicex;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.xml.namespace.QName;
    import javax.xml.ws.WebEndpoint;
    import javax.xml.ws.WebServiceClient;
    import javax.xml.ws.WebServiceFeature;
    import javax.xml.ws.Service;
    
    /**
     * This class was generated by Apache CXF 2.7.7
     * 2013-10-21T14:44:54.749-06:00
     * Generated source version: 2.7.7
     * 
     */
    @WebServiceClient(name = "StockQuote", 
                      wsdlLocation = "file:/home/axiopisty/projects/cxf-wsdl2java-example/src/main/resources/stockquote.wsdl",
                      targetNamespace = "http://www.webserviceX.NET/") 
    public class StockQuote extends Service {
    
        public final static URL WSDL_LOCATION;
    
        public final static QName SERVICE = new QName("http://www.webserviceX.NET/", "StockQuote");
        public final static QName StockQuoteHttpPost = new QName("http://www.webserviceX.NET/", "StockQuoteHttpPost");
        public final static QName StockQuoteSoap = new QName("http://www.webserviceX.NET/", "StockQuoteSoap");
        public final static QName StockQuoteSoap12 = new QName("http://www.webserviceX.NET/", "StockQuoteSoap12");
        public final static QName StockQuoteHttpGet = new QName("http://www.webserviceX.NET/", "StockQuoteHttpGet");
        static {
            URL url = null;
            try {
                url = new URL("file:/home/axiopisty/projects/cxf-wsdl2java-example/src/main/resources/stockquote.wsdl");
            } catch (MalformedURLException e) {
                java.util.logging.Logger.getLogger(StockQuote.class.getName())
                    .log(java.util.logging.Level.INFO, 
                         "Can not initialize the default wsdl from {0}", "file:/home/axiopisty/projects/cxf-wsdl2java-example/src/main/resources/stockquote.wsdl");
            }
            WSDL_LOCATION = url;
        }
    
        public StockQuote(URL wsdlLocation) {
            super(wsdlLocation, SERVICE);
        }
    
        public StockQuote(URL wsdlLocation, QName serviceName) {
            super(wsdlLocation, serviceName);
        }
    
        public StockQuote() {
            super(WSDL_LOCATION, SERVICE);
        }
    
        //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2
        //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1
        //compliant code instead.
        public StockQuote(WebServiceFeature ... features) {
            super(WSDL_LOCATION, SERVICE, features);
        }
    
        //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2
        //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1
        //compliant code instead.
        public StockQuote(URL wsdlLocation, WebServiceFeature ... features) {
            super(wsdlLocation, SERVICE, features);
        }
    
        //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2
        //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1
        //compliant code instead.
        public StockQuote(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
            super(wsdlLocation, serviceName, features);
        }
    
        /**
         *
         * @return
         *     returns StockQuoteHttpPost
         */
        @WebEndpoint(name = "StockQuoteHttpPost")
        public StockQuoteHttpPost getStockQuoteHttpPost() {
            return super.getPort(StockQuoteHttpPost, StockQuoteHttpPost.class);
        }
    
        /**
         * 
         * @param features
         *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the features parameter will have their default values.
         * @return
         *     returns StockQuoteHttpPost
         */
        @WebEndpoint(name = "StockQuoteHttpPost")
        public StockQuoteHttpPost getStockQuoteHttpPost(WebServiceFeature... features) {
            return super.getPort(StockQuoteHttpPost, StockQuoteHttpPost.class, features);
        }
        /**
         *
         * @return
         *     returns StockQuoteSoap
         */
        @WebEndpoint(name = "StockQuoteSoap")
        public StockQuoteSoap getStockQuoteSoap() {
            return super.getPort(StockQuoteSoap, StockQuoteSoap.class);
        }
    
        /**
         * 
         * @param features
         *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the features parameter will have their default values.
         * @return
         *     returns StockQuoteSoap
         */
        @WebEndpoint(name = "StockQuoteSoap")
        public StockQuoteSoap getStockQuoteSoap(WebServiceFeature... features) {
            return super.getPort(StockQuoteSoap, StockQuoteSoap.class, features);
        }
        /**
         *
         * @return
         *     returns StockQuoteSoap
         */
        @WebEndpoint(name = "StockQuoteSoap12")
        public StockQuoteSoap getStockQuoteSoap12() {
            return super.getPort(StockQuoteSoap12, StockQuoteSoap.class);
        }
    
        /**
         * 
         * @param features
         *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the features parameter will have their default values.
         * @return
         *     returns StockQuoteSoap
         */
        @WebEndpoint(name = "StockQuoteSoap12")
        public StockQuoteSoap getStockQuoteSoap12(WebServiceFeature... features) {
            return super.getPort(StockQuoteSoap12, StockQuoteSoap.class, features);
        }
        /**
         *
         * @return
         *     returns StockQuoteHttpGet
         */
        @WebEndpoint(name = "StockQuoteHttpGet")
        public StockQuoteHttpGet getStockQuoteHttpGet() {
            return super.getPort(StockQuoteHttpGet, StockQuoteHttpGet.class);
        }
    
        /**
         * 
         * @param features
         *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the features parameter will have their default values.
         * @return
         *     returns StockQuoteHttpGet
         */
        @WebEndpoint(name = "StockQuoteHttpGet")
        public StockQuoteHttpGet getStockQuoteHttpGet(WebServiceFeature... features) {
            return super.getPort(StockQuoteHttpGet, StockQuoteHttpGet.class, features);
        }
    }
    

    EDIT 9/11/2017

    Here is the content of the wsdl file just in case the website serving it ever goes away.

    stockquote.wsdl:

    
    
      
        
          
            
              
                
              
            
          
          
            
              
                
              
            
          
          
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
          Get Stock quote for a company Symbol
          
          
        
      
      
        
          Get Stock quote for a company Symbol
          
          
        
      
      
        
          Get Stock quote for a company Symbol
          
          
        
      
      
        
        
          
          
            
          
          
            
          
        
      
      
        
        
          
          
            
          
          
            
          
        
      
      
        
        
          
          
            
          
          
            
          
        
      
      
        
        
          
          
            
          
          
            
          
        
      
      
        
          
        
        
          
        
        
          
        
        
          
        
      
    
    

    0 讨论(0)
提交回复
热议问题