【发布时间】:2016-07-21 14:10:07
【问题描述】:
开箱即用的 Spyne 似乎将输出 JSON(P) 消息包装在一个数组中。与
out_protocol=JsonP('serviceResp', ignore_wrappers=True),
我明白了~
serviceResp([{ ... }]);
与
out_protocol=JsonP('serviceResp', ignore_wrappers=False),
它将它包装在一个结果/响应包装器中,
serviceResp({"appResponse": {"appResult": [{ ... }]);
我尝试设置 _body_style='bare',看看它是否有任何效果,但收到关于太多函数参数的错误 (??)。
Exception: body_style='bare' can handle at most one function argument.
我还尝试将 _returns= 设置为各种类型,但没有成功。
EDIT1:这里有一些示例代码实际上将结果包装在一个数组中。 EDIT2:修改了下面的代码以产生更简单的消息。
import logging
logging.basicConfig(level=logging.DEBUG)
from spyne.application import Application
from spyne.decorator import srpc
from spyne.service import ServiceBase
from spyne.model.primitive import Integer
from spyne.model.primitive import Unicode
from spyne.model.complex import Iterable
from spyne.protocol.http import HttpRpc
from spyne.protocol.json import JsonDocument
from spyne.server.wsgi import WsgiApplication
class HelloWorldService(ServiceBase):
@srpc(_returns=Unicode)
def say_hello():
yield 'Hello, James'
application = Application([HelloWorldService],
tns='spyne.examples.hello',
in_protocol=HttpRpc(validator='soft'),
out_protocol=JsonDocument()
)
if __name__ == '__main__':
# You can use any Wsgi server. Here, we chose
# Python's built-in wsgi server but you're not
# supposed to use it in production.
from wsgiref.simple_server import make_server
wsgi_app = WsgiApplication(application)
server = make_server('0.0.0.0', 8000, wsgi_app)
server.serve_forever()
["Hello, James"]
谁能解释如何从响应中删除数组包装器?
-詹姆斯
【问题讨论】: