如何在Node.js的Swagger UI(swagger.json)的标头中表示自定义标记

时间:2018-12-11 13:05:01

标签: node.js express swagger swagger-ui swagger-2.0

我正在ExpressJs中创建一个Restful服务器。我已经集成了swagger-jsdoc。以下是相关文件。下面(header.png)是我期望标题在大摇摇晃的UI中的样子。但是,当我打开swagger UI(http://localhost:3000/api-docs/)时,无法在标头中看到令牌标记(令牌和身份验证)。

enter image description here

swagger.json

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "Viswa API"
    },
    "host": "localhost:3000",
    "basePath": "/api",
    "tags": [{
        "name": "Customers",
        "description": "API for customers in the system"
    }],
    "schemes": [
        "http"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "securityDefinitions": {
        "Bearer": {
            "type": "apiKey",
            "name": "Authorization",
            "in": "header"
        },
        "JWT": {
            "type": "apiKey",
            "name": "token",
            "in": "header"
        }
    },
    "paths": {
        "/customer": {
            "post": {
                "tags": [
                    "Customers"
                ],
                "description": "Create new customer in system",
                "parameters": [{
                    "name": "customer",
                    "in": "body",
                    "description": "Customer that we want to create",
                    "schema": {
                        "$ref": "#/definitions/Customer"
                    }
                }],
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "201": {
                        "description": "New customer is created",
                        "schema": {
                            "$ref": "#/definitions/Customer"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "Customer": {
            "required": [
                "email"
            ],
            "properties": {
                "customer_name": {
                    "type": "string"
                },
                "customer_email": {
                    "type": "string"
                }
            }
        }
    }
}

app.route

var apiRoutes = express.Router();
app.use('/api', apiRoutes);
// swagger definition
var swaggerUi = require('swagger-ui-express'),
swaggerDocument = require('../swagger.json');

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/api/v1', apiRoutes);

当前Swagger用户界面:

1 个答案:

答案 0 :(得分:1)

您缺少安全标签。您可以在securityDefinitions标记下全局定义它,也可以为每个API端点定义一个。

看看this问题。