加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

axis-WebService传递复杂对象类型参数

发布时间:2020-12-17 00:55:50 所属栏目:安全 来源:网络整理
导读:WebService以前使用都是传递简单的字符串之类的参数,后面有项目需传递复杂对象类型参数,于是研究学习了下。 ? 使用jar包:axis-1.4.jar ? 接口类型代码片段:? Java代码?? //暂挂一个有效账号的服务 ?? public ?AccountResponse?suspendAccount(AccountReq



WebService以前使用都是传递简单的字符串之类的参数,后面有项目需传递复杂对象类型参数,于是研究学习了下。?

使用jar包:axis-1.4.jar?

接口类型代码片段:?
Java代码??

收藏代码

  1. //暂挂一个有效账号的服务??
  2. public?AccountResponse?suspendAccount(AccountRequest?accountInfo);??
  3. //检查一个有效账号的密码是否正确的服务??
  4. public?AccountResponse?validatePassword(AccountRequest?accountInfo);??
  5. 测试与Web?Service的通讯是否正常的服务??
  6. public?AccountResponse?testConnection(AccountRequest?accountInfo);??
  7. //修改一个有效账号可修改属性的服务??
  8. public?AccountResponse?modifyAccount(AccountRequest?accountInfo);??
  9. //恢复一个已暂挂账号的服务??
  10. public?AccountResponse?restoreAccount(AccountRequest?accountInfo);??
  11. //查询并返回全部或符合查询条件的账号集合的服务??
  12. public?SearchAccountsResponse?searchAccounts(AccountRequest?accountInfo);??
  13. //创建一个新账号的服务??
  14. public?AccountResponse?addAccount(AccountRequest?accountInfo);??
  15. //删除一个账号的服务??
  16. public?AccountResponse?deleteAccount(AccountRequest?accountInfo);??
  17. //修改一个有效账号密码的服务??
  18. public?AccountResponse?changePassword(AccountRequest?accountInfo);??


AccountRequest对象片段:?
public?class?AccountRequest?implements?Serializable{??
  • ????/**?
  • ?????*?请求唯一标识?字符串,每次请求唯一?
  • ?????*/??
  • ????private?String?requestID;??
  • ??????
  • ?????*?账号信息对象?用户数据对象?
  • private?AccountInfo?accountInfo;??
  • }??

  • 伦理片?http://www.dotdy .com/
    AccountResponse对象片段:?
    class?AccountResponse??????*?请求唯一标识?字符串,每次响应唯一,与请求时的ID保持一致?
  • ?????*?处理结果标识?true:处理成功?false:处理失败?
  • private?Boolean?returnFlag;??
  • ?????*?返回结果编号?由应用自行定义;?
  • ????????当returnFlag为true时,returnCode为0;?
  • ????????当returnFlag为false时,returnCode为错误编码?
  • private?String?returnCode;??
  • ?????*?返回结果信息?由应用自行定义,可放入提示、警告或错误信息描述?
  • private?String?returnMessage;??
  • }??


  • SearchAccountsResponse对象片段:?
    class?SearchAccountsResponse?{??
  • ??
  • private?AccountInfo[]?accountInfoList;??
  • private?Integer?accountInfoSize;??
  • }??


  • AccountInfo对象片段:?
    class?AccountInfo? ??????????????*?用户应用账号?应用账号ID?
  • private?String?accountID;??
  • ?????*?人员中文姓名?
  • private?String?cn;??
  • ?????*?应用登录密码?
  • private?String?userPassword;??
  • ?????*?兼职部门ID?
  • private?String[]?workOrg;??
  • ?????*?兼职部门名称?
  • private?String[]?workOrgName;??
  • }??


  • 使用 deploy.wsdd 生成service-config.wsdd:(deploy.wsdd 放于WEB-INF下)?
    我自己弄个批处理(复制到一个txt文件,保存为.bat文件即可运行):
    ?
    D:??
  • cd??D:workspaceEclipseAxisTestWebContentWEB-INF??
  • java?org.apache.axis.client.AdminClient?-lhttp://127.0.0.1:8080/AxisTest/services/AdminService?deploy.wsdd??
  • @echo?已经成功生成,代码在D:workspaceEclipseAxisTestWebContentWEB-INF目录下??
  • @echo?-----------------------------------------??
  • @pause??


  • deploy.wsdd:?
    Xml代码??

    收藏代码

      <?xml?version="1.0"?encoding="UTF-8"?>??
    1. <deployment?xmlns="http://xml.apache.org/axis/wsdd/"?xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">??
    2. ?service?name="exoaUmService"?provider="java:RPC"?style="wrapped"?use="literal" ????parameter?name="wsdlTargetNamespace"?value="http://cn.com"/>??
    3. ??????parameter?name="wsdlServiceElement"?value="exUmService"parameter?name="schemaQualified"?value="http://cn.com"parameter?name="wsdlServicePort"?value="exUmServiceHttpPort"parameter?name="className"?value="com.axis.test.ExUmService"parameter?name="wsdlPortType"?value="exUmServicePortType"parameter?name="typeMappingVersion"?value="1.2"operation?name="suspendAccount"?qname="operNS:suspendAccount"?xmlns:operNS="http://cn.com"?returnQName="retNS:out"?xmlns:retNS="http://cn.com"?returnType="rtns:AccountResponse"?xmlns:rtns="http://cn.com"?soapAction=""? ????????parameter?qname="pns:accountInfo"?xmlns:pns="http://cn.com"?type="tns:AccountRequest"?xmlns:tns="http://cn.com"</operationoperation?name="validatePassword"?qname="operNS:validatePassword"?xmlns:operNS="http://cn.com"?returnQName="retNS:out"?xmlns:retNS="http://cn.com"?returnType="rtns:AccountResponse"?xmlns:rtns="http://cn.com"?soapAction=""?operation?name="testConnection"?qname="operNS:testConnection"?xmlns:operNS="http://cn.com"?returnQName="retNS:out"?xmlns:retNS="http://cn.com"?returnType="rtns:AccountResponse"?xmlns:rtns="http://cn.com"?soapAction=""?operation?name="modifyAccount"?qname="operNS:modifyAccount"?xmlns:operNS="http://cn.com"?returnQName="retNS:out"?xmlns:retNS="http://cn.com"?returnType="rtns:AccountResponse"?xmlns:rtns="http://cn.com"?soapAction=""?operation?name="restoreAccount"?qname="operNS:restoreAccount"?xmlns:operNS="http://cn.com"?returnQName="retNS:out"?xmlns:retNS="http://cn.com"?returnType="rtns:AccountResponse"?xmlns:rtns="http://cn.com"?soapAction=""?operation?name="searchAccounts"?qname="operNS:searchAccounts"?xmlns:operNS="http://cn.com"?returnQName="retNS:out"?xmlns:retNS="http://cn.com"?returnType="rtns:SearchAccountsResponse"?xmlns:rtns="http://cn.com"?soapAction=""?operation?name="addAccount"?qname="operNS:addAccount"?xmlns:operNS="http://cn.com"?returnQName="retNS:out"?xmlns:retNS="http://cn.com"?returnType="rtns:AccountResponse"?xmlns:rtns="http://cn.com"?soapAction=""?operation?name="deleteAccount"?qname="operNS:deleteAccount"?xmlns:operNS="http://cn.com"?returnQName="retNS:out"?xmlns:retNS="http://cn.com"?returnType="rtns:AccountResponse"?xmlns:rtns="http://cn.com"?soapAction=""?operation?name="changePassword"?qname="operNS:changePassword"?xmlns:operNS="http://cn.com"?returnQName="retNS:out"?xmlns:retNS="http://cn.com"?returnType="rtns:AccountResponse"?xmlns:rtns="http://cn.com"?soapAction=""?parameter?name="allowedMethods"?value="*"typeMapping??
    4. ????????xmlns:ns="http://cn.com"??
    5. ????????qname="ns:SearchAccountsResponse"??
    6. ????????type="java:com.axis.test.SearchAccountsResponse"??
    7. ????????serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"??
    8. ????????deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"??
    9. ????????encodingStyle=""??
    10. ????????qname="ns:AccountInfo"??
    11. ????????type="java:com.axis.test.AccountInfo"??
    12. arrayMapping??
    13. ????????qname="ns:ArrayOfAccountInfo"??
    14. ????????type="java:com.axis.test.AccountInfo[]"??
    15. ????????innerType="cmp-ns:AccountInfo"?xmlns:cmp-ns="http://cn.com"??
    16. ????????qname="ns:AccountResponse"??
    17. ????????type="java:com.axis.test.AccountResponse"??
    18. ????????qname="ns:AccountRequest"??
    19. ????????type="java:com.axis.test.AccountRequest"??
    20. ????????qname="ns:ArrayOfString"??
    21. ????????type="java:java.lang.String[]"??
    22. ????????innerType="cmp-ns:string"?xmlns:cmp-ns="http://www.w3.org/2001/XMLSchema"??
    23. servicedeployment>??


    要注意那2个arrayMapping的设置。?

    服务发布成功后,访问你的服务,把显示的网页保存为.wsdl文件。?
    根据这个文件,就可以生成相应的代码。?
    这个功能我也弄了个批处理:
    ?
    set?Axis_Lib=F:axisaxis-1_4lib??
  • set?Java_Cmd=java?-Djava.ext.dirs=%Axis_Lib%??
  • set?Output_Path=F:axisservice??
  • set?Package=com.test.webservice??
  • %Java_Cmd%?org.apache.axis.wsdl.WSDL2Java?-o?%Output_Path%?-p?%Package%?-s?F:axisexUmService.wsdl??
  • @echo?客户端代码已经成功生成,代码在F:axisservice目录下??
  • @pause??


  • 执行上面的批处理,会在设置的Output_Path文件夹下面生成相关的代码,以及deploy.wsdd,undeploy.wsdd文件,根据这些文件,就可以调用前面发布的服务了。?

    影音先锋电影?http://www.iskdy .com/
    调用测试:?

    1、客户端调用。?
    ??? 这个就不搞了,有了客户端代码就很简单了。?

    2、Call方式调用,这个要说下。
    ?
    ??
    void?test(String?methodName,AccountRequest?accountInfo){??
  • ????Service?service?=?new?Service();??
  • ????Call?call?=?(Call)?service.createCall();??
  • ????call.setTargetEndpointAddress(new?java.net.URL(targetEendPoint));??
  • ??????????????
  • ????QName?qn?=?new?QName("http://cn.com",?"AccountInfo"?);????
  • ????call.registerTypeMapping(AccountInfo.class,qn,??
  • ????????????????????new?BeanSerializerFactory(AccountInfo.new?BeanDeserializerFactory(AccountInfo. ????QName?qn2?=?"AccountResponse"?);????
  • ????call.registerTypeMapping(AccountResponse.new?BeanSerializerFactory(AccountResponse.new?BeanDeserializerFactory(AccountResponse. ????QName?qn3?=?"AccountRequest"?);????
  • ????call.registerTypeMapping(AccountRequest.new?BeanSerializerFactory(AccountRequest.new?BeanDeserializerFactory(AccountRequest. ????QName?qn4?=?"SearchAccountsResponse"?);????
  • ????call.registerTypeMapping(SearchAccountsResponse.new?BeanSerializerFactory(SearchAccountsResponse.new?BeanDeserializerFactory(SearchAccountsResponse. ????QName?qn5?=?"ArrayOfAccountInfo"?);???
  • ????call.registerTypeMapping(AccountInfo[].new?ArraySerializerFactory(qn,?null),85); font-weight:bold">new?ArrayDeserializerFactory());//留意此处??
  • ????call.setOperationName( ????call.addParameter(?"arg1",?qn3,?ParameterMode.IN?);??
  • if("searchAccounts".equals(methodName)){??
  • ??????????????????
  • ????????call.setReturnClass(SearchAccountsResponse.class);???
  • ????????SearchAccountsResponse?result?=?(SearchAccountsResponse)?call.invoke(?new?Object[]?{accountInfo}?);???
  • ????????AccountInfo[]?att=result.getAccountInfoList();??
  • ????}else{??
  • ????????call.setReturnClass(AccountResponse. ????????AccountResponse?result?=?(AccountResponse)?call.invoke(? ????}??
  • }??
  • ?????


  • 上述都是代码片段,仅供参考。

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读