WSDL2Java通过WSDL文件生成java服务接口并实现

来源:互联网 发布:人工智能885728 编辑:程序博客网 时间:2024/04/30 01:21
 

1、  首先找到一个WSDL文件,可以去copy

    <?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions targetNamespace="http://address.jaxrpc.samples" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://address.jaxrpc.samples" xmlns:intf="http://address.jaxrpc.samples" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <wsdl:types>

  <schema targetNamespace="http://address.jaxrpc.samples" xmlns="http://www.w3.org/2001/XMLSchema">

   <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

   <complexType name="AddressBean">

    <sequence>

     <element name="street" nillable="true" type="xsd:string"/>

     <element name="postcode" type="xsd:int"/>

    </sequence>

   </complexType>

   <element name="AddressBean" nillable="true" type="impl:AddressBean"/>

  </schema>

  <schema targetNamespace="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/2001/XMLSchema">

   <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

   <element name="int" type="xsd:int"/>

   <element name="string" type="xsd:string"/>

  </schema>

 </wsdl:types>

 

   <wsdl:message name="updateAddressRequest">

 

      <wsdl:part name="in0" type="intf:AddressBean"/>

 

      <wsdl:part name="in1" type="xsd:int"/>

 

   </wsdl:message>

 

   <wsdl:message name="updateAddressResponse">

 

      <wsdl:part name="return" type="xsd:string"/>

 

   </wsdl:message>

 

   <wsdl:portType name="AddressService">

 

      <wsdl:operation name="updateAddress" parameterOrder="in0 in1">

 

         <wsdl:input message="intf:updateAddressRequest" name="updateAddressRequest"/>

 

         <wsdl:output message="intf:updateAddressResponse" name="updateAddressResponse"/>

 

      </wsdl:operation>

 

   </wsdl:portType>

 

   <wsdl:binding name="AddressSoapBinding" type="intf:AddressService">

 

      <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>

 

      <wsdl:operation name="updateAddress">

 

         <wsdlsoap:operation soapAction=""/>

 

         <wsdl:input name="updateAddressRequest">

 

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://address.jaxrpc.samples" use="encoded"/>

 

         </wsdl:input>

 

         <wsdl:output name="updateAddressResponse">

 

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://address.jaxrpc.samples" use="encoded"/>

 

         </wsdl:output>

 

      </wsdl:operation>

 

   </wsdl:binding>

 

   <wsdl:service name="AddressServiceService">

 

      <wsdl:port binding="intf:AddressSoapBinding" name="Address">

 

         <wsdlsoap:address location="http://localhost:8080/MyAxisPoJoWebService/services/Address"/>

 

      </wsdl:port>

 

   </wsdl:service>

 

</wsdl:definitions>

 

 

使用命令:

-p (指定包) -0(指定目录) -v(显示信息) --server-side(生成服务器绑定(可以不要)) --skeletonDeploy true   wsdl文件

-v -p cn.com.test.webservice.service -o src --server-side

Address.wsdl

会生成一些类和文件

 

加上--skeletonDeploy true的话表示指定实现类为部署类,上面会多一个AddressSoapBindingSkeleton.java

--skeletionDeploy true:可理解为对生成的骨架进行部署

 

然后将AddressSoapBindingInpl中的服务方法手动编码实现,通过deploy.wsdd进行上传到服务器

 

/**

 * AddressSoapBindingImpl.java

 *

 * This file was auto-generated from WSDL

 * by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.

 */

 

package cn.com.test.webservice.service;

 

public class AddressSoapBindingImpl implements cn.com.test.webservice.service.AddressService{

    public java.lang.String updateAddress(cn.com.test.webservice.service.AddressBean in0, int in1) throws java.rmi.RemoteException {

        return "测试返回数据";

    }

 

}

 

 

最后将所有生成的java文件拷贝到服务器端同目录下,也可适当看哪些不用进行删除

 

测试:

/*

 * Copyright 2001-2004 The Apache Software Foundation.

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

 

package cn.com.test.webservice;

 

import java.rmi.RemoteException;

 

import javax.xml.rpc.ServiceException;

 

import cn.com.test.webservice.service.AddressBean;

import cn.com.test.webservice.service.AddressService;

import cn.com.test.webservice.service.AddressServiceServiceLocator;

 

 

 

public class Client03

{

    public static void main(String [] args) throws ServiceException, RemoteException

    {

             AddressService service=   new AddressServiceServiceLocator().getAddress();

            

   

      System.out.println("张三"+service.updateAddress(new AddressBean(),2));

     

    }

}

 

原创粉丝点击