Web Service系列之常见问题

来源:互联网 发布:电缆分层算法 编辑:程序博客网 时间:2024/06/05 02:30

原文链接: Web Service系列之常见问题

问题: Server raised fault: ‘org.xml.sax.SAXException: No deserializer for {http://www.w3.org/2001/XMLSchema}anyType’

CSDN网友回复:
原因就是服务器端将对象序列化为SOAP消息, 到了客户端后进行反序列化, 这个时候在SOAP消息中的某个元素是anyType
类型, 但是和客户端在命名空间中的注册属性无法匹配, 导致不能反序列化, 大部分是命名空间的问题

stackoverflow网友回复:
http://stackoverflow.com/questions/13593950/suds-write-request-missing-datatype
I managed to solve it, using the answer from Adding xsi:type and envelope namespace when using SUDS ( http://stackoverflow.com/a/10977734/696768 )
I am not sure this is the only possible solution, and to me it seems more of a hack than anything else, however it will work fine for my current scenario.
The solution i used, is making a plugin for the client, looking for the particular element that i need to be xsi:type=”xsd:int”, then adding these attributes to those elements.
The code i ended up using for reference (from the aforementioned stackoverflow question with minor adjustments):

from suds.plugin import MessagePluginfrom suds.sax.attribute import Attributeclass SoapFixer(MessagePlugin):    def marshalled(self, context):        # Alter the envelope so that the xsd namespace is allowed        context.envelope.nsprefixes['xsd'] = 'http://www.w3.org/2001/XMLSchema'        # Go through every node in the document and apply the fix function to patch up incompatible XML.         context.envelope.walk(self.fix_any_type_string)    def fix_any_type_string(self, element):        """Used as a filter function with walk in order to fix errors.        If the element has a certain name, give it a xsi:type=xsd:int. Note that the nsprefix xsd must also         be added in to make this work."""        # Fix elements which have these names        fix_names = ['Value', 'anotherelementname']        if element.name in fix_names:            element.attributes.append(Attribute('xsi:type', 'xsd:int'))plugin=SoapFixer()

Then I added plugins=[plugin] to the client ctor.
Example:

client = suds.client.Client("http://127.0.0.1:8099/TagValueWriteService?wsdl",plugins=[plugin])

补充:
https://wiki.python.org/moin/WebServices
Web Service: JSON-RPC JSON-WSP SOAP SOAPjr XML-RPC XMPP

0 0
原创粉丝点击