我正在尝试获取swagger project create
生成的“开箱即用”的hello world项目,以返回xml响应而不是json。这是我已采取的步骤:
1)运行swagger project create
并选择快递选项。
2)打开swagger.yaml并进行以下更改:
# format of the responses to the client (Accepts)
produces:
- application/xml
如果您对此感兴趣,这里是整个swagger.yaml:
swagger: "2.0"
info:
version: "0.0.1"
title: Hello World App
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/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
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/HelloWorldResponse"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
HelloWorldResponse:
required:
- message
properties:
message:
type: string
ErrorResponse:
required:
- message
properties:
message:
type: string
3)打开hello_world.js控制器,并将res对象更改为非json,并将类型设置为application/xml
。我还将jstoxml添加到项目中,以将对象转换为xml。这是代码:
'use strict';
var util = require('util');
const { toXML } = require('jstoxml');
module.exports = {
hello: hello
};
function hello(req, res) {
// 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 = {message: 'hello'};
var xml = toXML(hello);
res.type('application/xml');
res.end(xml);
}
4)运行swagger project start
,然后运行以下curl请求:
curl -X GET "http://127.0.0.1:10010/hello?name=Scott" -H "accept: application/xml"
5)得到了以下答复:
{"message":"Response validation failed: value expected to be an array/object but is not","failedValidation":true,"originalResponse":"<message>hello</message>"}
服务器输出以下错误:
SyntaxError: Response validation failed: value expected to be an array/object but is not
at JSON.parse (<anonymous>)
at validateValue (/Users/seanroberts/projects/hello-xml/node_modules/swagger-tools/middleware/swagger-validator.js:125:20)
at ServerResponse.res.end (/Users/seanroberts/projects/hello-xml/node_modules/swagger-tools/middleware/swagger-validator.js:252:9)
at hello (/Users/seanroberts/projects/hello-xml/api/controllers/hello_world.js:45:7)
at swaggerRouter (/Users/seanroberts/projects/hello-xml/node_modules/swagger-tools/middleware/swagger-router.js:407:20)
at swagger_router (/Users/seanroberts/projects/hello-xml/node_modules/swagger-node-runner/fittings/swagger_router.js:31:5)
at Runner.<anonymous> (/Users/seanroberts/projects/hello-xml/node_modules/bagpipes/lib/bagpipes.js:171:7)
at bound (domain.js:301:14)
at Runner.runBound (domain.js:314:12)
at Runner.pipeline (/Users/seanroberts/projects/hello-xml/node_modules/pipeworks/pipeworks.js:72:17)
我在做什么错了?
答案 0 :(得分:0)
如何删除swagger.yaml中的验证部分:
definitions:
HelloWorldResponse:
required:
- message
properties:
message:
type: string
并这样放置:
definitions:
HelloWorldResponse:
type: string