【问题标题】:How to produce/consume XML using swagger-node如何使用 swagger-node 生成/使用 XML
【发布时间】:2015-10-02 01:51:24
【问题描述】:

我是使用 swagger-node (swagger-spec 2.0) 的新手,我需要我的 API 来使用和生成 XML 和 JSON(因为这是客户想要的)。目前我只关注“生产”部分。

在生成响应时,我知道我可以使用 jstoxmleasyxml 等工具将我的 js 对象转换为 XML。所以问题是:在使用 swagger-node 时这是否必要,或者工具是否应该处理这个问题?我想我需要帮助我的控制器代码应该返回什么。

例如,使用 swagger 创建一个新项目 swagger project create myproject (choose express framework)

更改 /hello api 的 yaml 文件,以便 get: 返回 json 或 xml

paths:
  /hello:
    # binds a127 app logic to a route
    x-swagger-router-controller: hello_world
    get:
      description: Returns 'Hello' to the caller
      # used as the method name of the controller
      operationId: hello
      produces:
        - application/json
        - application/xml

然后更改 hello_world.js 控制器以返回 json 对象而不是字符串

  // variables defined in the Swagger document can be referenced using req.swagger.params.{parameter_name}
  var name = req.swagger.params.name.value || 'stranger';
  var hello = util.format('Hello, %s!', name);

  // this sends back a JSON response which is a single string
  res.json({message:hello});
}

当我启动项目并使用带有 Header Accept = application/json 的 Postman 我得到了回应:

{
    "message": "Hello, stranger!"
}

如果我更改标头 Accept application/xml,我仍然会收到 JSON 响应,而不是 XML。我希望看到的是:

<object>
<message>Hello, stranger!</message>
</object>

我知道我的代码使用 res.json() 是错误的,因为我相信它将 Content-Type 设置为 application/json

我不知道还可以使用什么来生成 XML 响应。当我更改 res.json() 以使用 easyxml

var xml = easyxml.render({message:hello});
res.type('xml').end(xml);

然后我从 swagger 那里得到一个验证错误:

[
  {
    "status": 500,
    "message": "Response validation failed: value expected to be an array/object but is not"
  }
]

那么我的控制器应该如何格式化响应以返回 XML 或 JSON?

【问题讨论】:

    标签: json xml node.js swagger swagger-2.0


    【解决方案1】:

    我不确定 easyxml 正在做什么或为什么它不起作用,但 jstoxml 效果很好:

    var jstoxml = require('jstoxml');
    var express = require('express');
    var util = require('util');
    var app = express();
    
    app.get('/', function(req, res) {
        var name = 'stranger';
        var hello = {
            object: {
                message: util.format('Hello, %s!', name)
            }
        };
        if (req.headers.accept === 'application/xml') {
            res.type('xml')
            res.end(jstoxml.toXML(hello));
        } else {
            res.json(hello);
        }
    });
    
    app.listen(process.env.PORT || 8100);
    

    接受:application/json

    {
        "object": 
        {
            "message": "Hello, stranger!"
        }
    }
    

    接受:应用程序/xml

    <object>
        <message>Hello, stranger!</message>
    </object>
    

    【讨论】:

    • 我相信问题不仅仅是让 express 以有效的 xml 响应,而且我从 swagger 那里得到了与 Greg 相同的验证错误(使用 js2xmlparser 生成 xml)。我认为这与 swagger.yaml 或 swagger.json 文件未正确定义响应模式有关,因为它似乎需要一个对象或数组而不是 xml 字符串。在我的 swagger.yaml 中,我尝试添加以下内容,但仍然没有运气:produces: - application/json - application/xml
    【解决方案2】:

    显然有没有正确的方法。 remus 的回答没有考虑到Swagger explicit XML support。 在任何情况下,控制器绝对不应该弄乱 API i/o 格式,而只是实现业务逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      相关资源
      最近更新 更多