webService生成java客户端代码踩坑(省流版)

一什么是webService

Web Service也称为web服务,它是一种跨编程语言和操作系统平台的远程调用技术。
Web Service采用标准的SOAP协议传输(SOAP:Simple Object Access Protocol简单对象访问协议,soap属于w3c标准。并且soap协议是基于http的应用层协议传输xml数据)。
Web Service采用WSDL作为描述语言,也就是Web Service 的使用说明书。
并且W3C为Web Service制定了一套传输数据类型,使用xml进行描述,即XSD(XML Schema Datatypes),任何语言写的web Service 接口在发送数据的时候都要转换成WebService标准的XSD发送。

二wsdl是什么

WSDL 是基于 XML 的用于描述 Web Services 以及如何访问 Web Services 的语言
可以访问别人发布的webService服务
如http://xxxxx:8089/myservice?wsdl 看到一个xml,也可以另存下来得到一个xml,
有时xml会写明通过什么方式发布的,如下图 Apache Axis version: 1.4

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://com.bdcc.hoffice.webservice.WebService" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://com.bdcc.hoffice.webservice.WebService" xmlns:intf="http://com.bdcc.hoffice.webservice.WebService" 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 created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->

   <wsdl:message name="medicalInstitutionsSubscribeServiceResponse">

      <wsdl:part name="medicalInstitutionsSubscribeServiceReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="transferTreatReferralServiceResponse">

      <wsdl:part name="transferTreatReferralServiceReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="hospitalWritebackServiceResponse">

      <wsdl:part name="hospitalWritebackServiceReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="platformServiceRequest">

      <wsdl:part name="typeno" type="soapenc:string"/>

      <wsdl:part name="inarg" type="soapenc:string"/>

      <wsdl:part name="userinfo" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="TestReportsServiceResponse">

      <wsdl:part name="TestReportsServiceReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="hospitalWritebackServiceRequest">

      <wsdl:part name="typeno" type="soapenc:string"/>

      <wsdl:part name="inarg" type="soapenc:string"/>

      <wsdl:part name="userinfo" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="outpatientRegisterRequest">

      <wsdl:part name="typeno" type="soapenc:string"/>

      <wsdl:part name="inarg" type="soapenc:string"/>

      <wsdl:part name="userinfo" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="familyDoctorServiceResponse">

      <wsdl:part name="familyDoctorServiceReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="outpatientRegisterResponse">

      <wsdl:part name="outpatientRegisterReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="platformServiceResponse">

      <wsdl:part name="platformServiceReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="subscribServiceResponse">

      <wsdl:part name="subscribServiceReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="medicalInstitutionsSubscribeServiceRequest">

      <wsdl:part name="typeno" type="soapenc:string"/>

      <wsdl:part name="inarg" type="soapenc:string"/>

      <wsdl:part name="userinfo" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="transferTreatReferralServiceRequest">

      <wsdl:part name="typeno" type="soapenc:string"/>

      <wsdl:part name="inarg" type="soapenc:string"/>

      <wsdl:part name="userinfo" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="smsServiceRequest">

      <wsdl:part name="typeno" type="soapenc:string"/>

      <wsdl:part name="inarg" type="soapenc:string"/>

      <wsdl:part name="userinfo" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="familyDoctorServiceRequest">

      <wsdl:part name="typeno" type="soapenc:string"/>

      <wsdl:part name="inarg" type="soapenc:string"/>

      <wsdl:part name="userinfo" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="smsServiceResponse">

      <wsdl:part name="smsServiceReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="subscribServiceRequest">

      <wsdl:part name="typeno" type="soapenc:string"/>

      <wsdl:part name="inarg" type="soapenc:string"/>

      <wsdl:part name="userinfo" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="TestReportsServiceRequest">

      <wsdl:part name="typeno" type="soapenc:string"/>

      <wsdl:part name="inarg" type="soapenc:string"/>

      <wsdl:part name="userinfo" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:portType name="WebService">

      <wsdl:operation name="smsService" parameterOrder="typeno inarg userinfo">

         <wsdl:input message="impl:smsServiceRequest" name="smsServiceRequest"/>

         <wsdl:output message="impl:smsServiceResponse" name="smsServiceResponse"/>

      </wsdl:operation>

      <wsdl:operation name="outpatientRegister" parameterOrder="typeno inarg userinfo">

         <wsdl:input message="impl:outpatientRegisterRequest" name="outpatientRegisterRequest"/>

         <wsdl:output message="impl:outpatientRegisterResponse" name="outpatientRegisterResponse"/>

      </wsdl:operation>

      <wsdl:operation name="subscribService" parameterOrder="typeno inarg userinfo">

         <wsdl:input message="impl:subscribServiceRequest" name="subscribServiceRequest"/>

         <wsdl:output message="impl:subscribServiceResponse" name="subscribServiceResponse"/>

      </wsdl:operation>

      <wsdl:operation name="hospitalWritebackService" parameterOrder="typeno inarg userinfo">

         <wsdl:input message="impl:hospitalWritebackServiceRequest" name="hospitalWritebackServiceRequest"/>

         <wsdl:output message="impl:hospitalWritebackServiceResponse" name="hospitalWritebackServiceResponse"/>

      </wsdl:operation>

      <wsdl:operation name="medicalInstitutionsSubscribeService" parameterOrder="typeno inarg userinfo">

         <wsdl:input message="impl:medicalInstitutionsSubscribeServiceRequest" name="medicalInstitutionsSubscribeServiceRequest"/>

         <wsdl:output message="impl:medicalInstitutionsSubscribeServiceResponse" name="medicalInstitutionsSubscribeServiceResponse"/>

      </wsdl:operation>

      <wsdl:operation name="transferTreatReferralService" parameterOrder="typeno inarg userinfo">

         <wsdl:input message="impl:transferTreatReferralServiceRequest" name="transferTreatReferralServiceRequest"/>

         <wsdl:output message="impl:transferTreatReferralServiceResponse" name="transferTreatReferralServiceResponse"/>

      </wsdl:operation>

      <wsdl:operation name="TestReportsService" parameterOrder="typeno inarg userinfo">

         <wsdl:input message="impl:TestReportsServiceRequest" name="TestReportsServiceRequest"/>

         <wsdl:output message="impl:TestReportsServiceResponse" name="TestReportsServiceResponse"/>

      </wsdl:operation>

      <wsdl:operation name="familyDoctorService" parameterOrder="typeno inarg userinfo">

         <wsdl:input message="impl:familyDoctorServiceRequest" name="familyDoctorServiceRequest"/>

         <wsdl:output message="impl:familyDoctorServiceResponse" name="familyDoctorServiceResponse"/>

      </wsdl:operation>

      <wsdl:operation name="platformService" parameterOrder="typeno inarg userinfo">

         <wsdl:input message="impl:platformServiceRequest" name="platformServiceRequest"/>

         <wsdl:output message="impl:platformServiceResponse" name="platformServiceResponse"/>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="WebServiceSoapBinding" type="impl:WebService">

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

      <wsdl:operation name="smsService">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="smsServiceRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.hoffice.bdcc.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="smsServiceResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://com.bdcc.hoffice.webservice.WebService" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="outpatientRegister">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="outpatientRegisterRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.hoffice.bdcc.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="outpatientRegisterResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://com.bdcc.hoffice.webservice.WebService" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="subscribService">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="subscribServiceRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.hoffice.bdcc.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="subscribServiceResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://com.bdcc.hoffice.webservice.WebService" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="hospitalWritebackService">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="hospitalWritebackServiceRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.hoffice.bdcc.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="hospitalWritebackServiceResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://com.bdcc.hoffice.webservice.WebService" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="medicalInstitutionsSubscribeService">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="medicalInstitutionsSubscribeServiceRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.hoffice.bdcc.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="medicalInstitutionsSubscribeServiceResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://com.bdcc.hoffice.webservice.WebService" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="transferTreatReferralService">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="transferTreatReferralServiceRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.hoffice.bdcc.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="transferTreatReferralServiceResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://com.bdcc.hoffice.webservice.WebService" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="TestReportsService">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="TestReportsServiceRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.hoffice.bdcc.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="TestReportsServiceResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://com.bdcc.hoffice.webservice.WebService" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="familyDoctorService">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="familyDoctorServiceRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.hoffice.bdcc.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="familyDoctorServiceResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://com.bdcc.hoffice.webservice.WebService" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="platformService">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="platformServiceRequest">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.hoffice.bdcc.com" use="encoded"/>

         </wsdl:input>

         <wsdl:output name="platformServiceResponse">

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://com.bdcc.hoffice.webservice.WebService" use="encoded"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="WebService">

      <wsdl:port binding="impl:WebServiceSoapBinding" name="WebService">

         <wsdlsoap:address location="http://10.10.2.46:8096/hoffice/services/WebService"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>

三SOAP协议

目前的主流版本为:SOAP1.1和SOAP1.2(soap1.2是被纳入w3c标准后的版本)
Jaxws支持SOAP1.1服务端发布
SOAP1.2的服务端呢1.需要导入第三方jar包(jaxws-ri-2.2.8)2.在实现类中添加@BindingType(SOAP12HTTP_BINDING)注解
1.数据格式不同:content-type不同。

     SOAP1.1:text/xml;charset=utf-8

     SOAP1.2:application/soap+xml;charset=utf-8

2.命名空间不同。

     SOAP1.1:http://schemas.xmlsoap.org/soap/envelope/

     SOAP1.2:http://www.w3.org/2003/05/soap-envelope

四生成客户端代码

1wsimport 原生方式

两种方式都行
wsimport -s . hello.xml
wsimport -s . http://192.168.1.100:6789/hello?wsdl 

会生成一些自定义的实体类 ,webService接口 如



@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AuthKey", propOrder = {
    "authKey",
    "expiryTime",
    "macAddress",
    "orgCode"
})
public class AuthKey
    extends WSResult
{
    /**
     * 认证字符串
     */
    protected String authKey;
    /**
     * 有效时间
     */
    protected String expiryTime;
    /**
     * MAC地址
     */
    protected String macAddress;
    /**
     * 机构代码
     */
    protected String orgCode;

    /**
     * 获取authKey属性的值。
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getAuthKey() {
        return authKey;
    }

    /**
     * 设置authKey属性的值。
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setAuthKey(String value) {
        this.authKey = value;
    }

    /**
     * 获取expiryTime属性的值。
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getExpiryTime() {
        return expiryTime;
    }

    /**
     * 设置expiryTime属性的值。
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setExpiryTime(String value) {
        this.expiryTime = value;
    }

    /**
     * 获取macAddress属性的值。
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getMacAddress() {
        return macAddress;
    }

    /**
     * 设置macAddress属性的值。
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setMacAddress(String value) {
        this.macAddress = value;
    }

    /**
     * 获取orgCode属性的值。
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getOrgCode() {
        return orgCode;
    }

    /**
     * 设置orgCode属性的值。
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setOrgCode(String value) {
        this.orgCode = value;
    }

}

通过getPort()调用

       //创建服务视图对象
        FsCardService_Service fsCardService_service=new FsCardService_Service();
        //获取服务实现类
        FsCardService fsCardService=fsCardService_service.getFSCardServiceImplPort();
        //调用方法
        AuthKey anthkey=fsCardService.getAuthKey(orgCode,userName,passWord);

2axis方式
第一种方式有时会报错

不支持使用 SOAP 编码。SOAP 扩展元素包含 XXXX

参考博客
https://blog.csdn.net/lw112190/article/details/110920699
axis下载地址:axis
此时还是报错

Unable to find required classes (javax.activation.DataHandler and
javax.mail.internet.MimeMultipart)

解决办法
https://blog.csdn.net/zhengqiqiqinqin/article/details/19341487
生成客户端代码命令

java -cp mail.jar;saaj.jar;jaxrpc.jar;commons-discovery-0.2.jar;commons-logging-1.0.4.jar;axis.jar;activation.jar;wsdl4j-1.5.1.jar org.apache.axis.wsdl.WSDL2Java D:\hello.xml -p com.qtkldsq.csdn
       <dependency>
            <groupId>axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-wsdl4j</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

调用

        WebService_ServiceLocator wb=WebService_ServiceLocator.init();
        WebServiceSoapBindingStub  ws=(WebServiceSoapBindingStub)wb.getWebService();
        Map map=new HashMap();
        map.put("orgId","1");
        map.put("geogId","1");
        String xml= XmlUtil.MapToXML(map);
        String responseXml=ws.smsService("900911","1|999",xml);

三 apache-cxf

在这里插入图片描述
同样也是下载相关jar生成,不赘述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值