LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1080|回复: 4

[求助]问两个SAAJ编程的问题,高手们帮忙 -- 急

[复制链接]
发表于 2006-7-21 16:18:23 | 显示全部楼层 |阅读模式
1.怎么在<soapenv:Envelope>元素里添加而外的namespace,用SAAJ的API生成SOAP包,<soapenv:Envelope>里只有一个默认的namespace

2.怎么设置元素不同类型的值,我在api中只看到addTextNode()的这个方法,可是这个方法添加的只是String类型的,怎么可以添加比如boolean,以及自己定义的一些类型?

问题很急,希望大家帮忙
毕业上班的第一个任务,希望能够完成的出色点
发表于 2006-7-21 16:49:52 | 显示全部楼层
SAAJ是什么?
SOAP?
呵呵。
正好我最近也有一个SOAP的东西在弄。
不知道楼上用的是啥工具?
我目前了解了两种:
Perl的SOAP:ite和java的axis.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-21 17:46:37 | 显示全部楼层
就是SOAP啦,我想自己手动写一个客户端程序,连接到已经规定好服务端上
以Google的API为例,我想不用Google的api,自己手动写一个满足google的wsdl文件规定的客户端程序,但是编写的时候就遇到上面的两个问题了,第一问题解决了一半,第二个问题毫无头绪,我把SAAJ的的API看了遍,但是就是没有看到怎么样添加不同类型的元素值,就看到了一个addTextNode(),但是google的wsdl里规定的还有boolean类型,还有int类型,不知道要怎么做,如果只用addTextNode()方法发出去,收到的就是类型错误的<soapfault>代码的回复

顺便贴一下我写的代码,很糙就是了

  1. import javax.xml.soap.*;
  2. import javax.xml.messaging.*;
  3. import javax.xml.namespace.QName;

  4. public class GoogleSearchClient{
  5.         public static final String DEFAULT_HOST_URL = "http://api.google.com/search/beta2";
  6.        
  7.         public static void main(String[] args){
  8.                 try{
  9.                        
  10.                         // Create the connection
  11.                         SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
  12.                         SOAPConnection connection = scf.createConnection();
  13.                        
  14.                         // create a empty context message
  15.                         MessageFactory mf = MessageFactory.newInstance();
  16.                         SOAPMessage message = mf.createMessage();
  17.                        
  18.                         // create objects for the message
  19.                         SOAPPart soapPart = message.getSOAPPart();
  20.                         SOAPEnvelope envelope = soapPart.getEnvelope();
  21.                         SOAPBody body = envelope.getBody();
  22.                        
  23.                         //QName qname = new QName("urn:GoogleSearch","GoogleSearchPort");
  24.                         Name name = envelope.createName("doGoogleSearch","urn:GoogleSearch","GoogleSearchPort");
  25.                        
  26.                         SOAPElement bodyElement = body.addChildElement(name);
  27.                         // add message context of doGoogleSearch
  28.                         bodyElement.addChildElement("key").addTextNode("FKwUhSBQFHItwJcf5LxGsjKMyhC0cggp");
  29.                         bodyElement.addChildElement("q").addTextNode("Linux");
  30.                         bodyElement.addChildElement("start").addTextNode("0");
  31.                         bodyElement.addChildElement("maxResults").addTextNode("10");
  32.                         bodyElement.addChildElement("filter").addTextNode("false");
  33.                         bodyElement.addChildElement("restrict");
  34.                         bodyElement.addChildElement("safeSearch").addTextNode("false");
  35.                         bodyElement.addChildElement("lr");
  36.                         bodyElement.addChildElement("ie");
  37.                         bodyElement.addChildElement("oe");
  38.                        
  39.                         message.saveChanges();
  40.                        
  41.                         // Output the message
  42.                         System.out.println("message:");
  43.                         message.writeTo(System.out);
  44.                         System.out.println();
  45.                        
  46.                         URLEndpoint url = new URLEndpoint(DEFAULT_HOST_URL);
  47.                         SOAPMessage reply = connection.call(message, url);
  48.                        
  49.                         // check the response
  50.                         System.out.println("the response");
  51.                         reply.writeTo(System.out);
  52.                
  53.                         connection.close();
  54.                 }catch(Throwable e){
  55.                         e.printStackTrace();
  56.                 }
  57.         }
  58. }               
复制代码


还有就是google的wsdl文件,http://api.google.com/GoogleSearch.wsdl

  1. <?xml version="1.0" ?>
  2. - <!--
  3. WSDL description of the Google Web APIs.
  4.      The Google Web APIs are in beta release. All interfaces are subject to
  5.      change as we refine and extend our APIs. Please see the terms of use
  6.      for more information.

  7.   -->
  8. - <!--  Revision 2002-08-16
  9.   -->
  10. - <definitions name="GoogleSearch" targetNamespace="urn:GoogleSearch" xmlns:typens="urn:GoogleSearch" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
  11. - <!--  Types for search - result elements, directory categories
  12.   -->
  13. - <types>
  14. - <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:GoogleSearch">
  15. - <xsd:complexType name="GoogleSearchResult">
  16. - <xsd:all>
  17.   <xsd:element name="documentFiltering" type="xsd:boolean" />
  18.   <xsd:element name="searchComments" type="xsd:string" />
  19.   <xsd:element name="estimatedTotalResultsCount" type="xsd:int" />
  20.   <xsd:element name="estimateIsExact" type="xsd:boolean" />
  21.   <xsd:element name="resultElements" type="typens:ResultElementArray" />
  22.   <xsd:element name="searchQuery" type="xsd:string" />
  23.   <xsd:element name="startIndex" type="xsd:int" />
  24.   <xsd:element name="endIndex" type="xsd:int" />
  25.   <xsd:element name="searchTips" type="xsd:string" />
  26.   <xsd:element name="directoryCategories" type="typens:DirectoryCategoryArray" />
  27.   <xsd:element name="searchTime" type="xsd:double" />
  28.   </xsd:all>
  29.   </xsd:complexType>
  30. - <xsd:complexType name="ResultElement">
  31. - <xsd:all>
  32.   <xsd:element name="summary" type="xsd:string" />
  33.   <xsd:element name="URL" type="xsd:string" />
  34.   <xsd:element name="snippet" type="xsd:string" />
  35.   <xsd:element name="title" type="xsd:string" />
  36.   <xsd:element name="cachedSize" type="xsd:string" />
  37.   <xsd:element name="relatedInformationPresent" type="xsd:boolean" />
  38.   <xsd:element name="hostName" type="xsd:string" />
  39.   <xsd:element name="directoryCategory" type="typens:DirectoryCategory" />
  40.   <xsd:element name="directoryTitle" type="xsd:string" />
  41.   </xsd:all>
  42.   </xsd:complexType>
  43. - <xsd:complexType name="ResultElementArray">
  44. - <xsd:complexContent>
  45. - <xsd:restriction base="soapenc:Array">
  46.   <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="typens:ResultElement[]" />
  47.   </xsd:restriction>
  48.   </xsd:complexContent>
  49.   </xsd:complexType>
  50. - <xsd:complexType name="DirectoryCategoryArray">
  51. - <xsd:complexContent>
  52. - <xsd:restriction base="soapenc:Array">
  53.   <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="typens:DirectoryCategory[]" />
  54.   </xsd:restriction>
  55.   </xsd:complexContent>
  56.   </xsd:complexType>
  57. - <xsd:complexType name="DirectoryCategory">
  58. - <xsd:all>
  59.   <xsd:element name="fullViewableName" type="xsd:string" />
  60.   <xsd:element name="specialEncoding" type="xsd:string" />
  61.   </xsd:all>
  62.   </xsd:complexType>
  63.   </xsd:schema>
  64.   </types>
  65. - <!--  Messages for Google Web APIs - cached page, search, spelling.
  66.   -->
  67. - <message name="doGetCachedPage">
  68.   <part name="key" type="xsd:string" />
  69.   <part name="url" type="xsd:string" />
  70.   </message>
  71. - <message name="doGetCachedPageResponse">
  72.   <part name="return" type="xsd:base64Binary" />
  73.   </message>
  74. - <message name="doSpellingSuggestion">
  75.   <part name="key" type="xsd:string" />
  76.   <part name="phrase" type="xsd:string" />
  77.   </message>
  78. - <message name="doSpellingSuggestionResponse">
  79.   <part name="return" type="xsd:string" />
  80.   </message>
  81. - <!--  note, ie and oe are ignored by server; all traffic is UTF-8.
  82.   -->
  83. - <message name="doGoogleSearch">
  84.   <part name="key" type="xsd:string" />
  85.   <part name="q" type="xsd:string" />
  86.   <part name="start" type="xsd:int" />
  87.   <part name="maxResults" type="xsd:int" />
  88.   <part name="filter" type="xsd:boolean" />
  89.   <part name="restrict" type="xsd:string" />
  90.   <part name="safeSearch" type="xsd:boolean" />
  91.   <part name="lr" type="xsd:string" />
  92.   <part name="ie" type="xsd:string" />
  93.   <part name="oe" type="xsd:string" />
  94.   </message>
  95. - <message name="doGoogleSearchResponse">
  96.   <part name="return" type="typens:GoogleSearchResult" />
  97.   </message>
  98. - <!--  Port for Google Web APIs, "GoogleSearch"
  99.   -->
  100. - <portType name="GoogleSearchPort">
  101. - <operation name="doGetCachedPage">
  102.   <input message="typens:doGetCachedPage" />
  103.   <output message="typens:doGetCachedPageResponse" />
  104.   </operation>
  105. - <operation name="doSpellingSuggestion">
  106.   <input message="typens:doSpellingSuggestion" />
  107.   <output message="typens:doSpellingSuggestionResponse" />
  108.   </operation>
  109. - <operation name="doGoogleSearch">
  110.   <input message="typens:doGoogleSearch" />
  111.   <output message="typens:doGoogleSearchResponse" />
  112.   </operation>
  113.   </portType>
  114. - <!--  Binding for Google Web APIs - RPC, SOAP over HTTP
  115.   -->
  116. - <binding name="GoogleSearchBinding" type="typens:GoogleSearchPort">
  117.   <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
  118. - <operation name="doGetCachedPage">
  119.   <soap:operation soapAction="urn:GoogleSearchAction" />
  120. - <input>
  121.   <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  122.   </input>
  123. - <output>
  124.   <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  125.   </output>
  126.   </operation>
  127. - <operation name="doSpellingSuggestion">
  128.   <soap:operation soapAction="urn:GoogleSearchAction" />
  129. - <input>
  130.   <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  131.   </input>
  132. - <output>
  133.   <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  134.   </output>
  135.   </operation>
  136. - <operation name="doGoogleSearch">
  137.   <soap:operation soapAction="urn:GoogleSearchAction" />
  138. - <input>
  139.   <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  140.   </input>
  141. - <output>
  142.   <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  143.   </output>
  144.   </operation>
  145.   </binding>
  146. - <!--  Endpoint for Google Web APIs
  147.   -->
  148. - <service name="GoogleSearchService">
  149. - <port name="GoogleSearchPort" binding="typens:GoogleSearchBinding">
  150.   <soap:address location="http://api.google.com/search/beta2" />
  151.   </port>
  152.   </service>
  153.   </definitions>
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-21 17:49:39 | 显示全部楼层
我的目的比较简单,我只想完成 符合wsdl文档规定的客户端
可以发送soap包,然后收到回复就可以了
不必要完成服务端的配置及实现

顺带说一下: 我自己学习的时候用的是apache axis
我是刚开始学Web Service,概念上还是很不清楚,经常一头雾水
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-21 22:32:21 | 显示全部楼层
解决了,我namespace在编写的时候写错了位置了,解决了,呼呼,任务快完成了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表