我可以在REST API中为不同的方法编写相同的路径名吗?

时间:2017-08-05 05:28:25

标签: node.js rest express swagger

我正在使用Swagger api编写REST API。现在,我想以下列方式编写插入,更新和删除方法(我已在许多API上看到过这样的路径):

  1. 发布(用于插入)
  2. /生

    1. PUT (更新时)
    2. /生/ {studentId}

      1. 删除(删除)
      2. /生/ {studentId}

        现在,1可以,但2和3是相同的路径(但有不同的方法)。当我写这篇文章时,Swagger API给出了错误。这真的不允许吗?

        如果不允许,有哪些最好的方法可以定义不同的路径,而不会让它们变长,看起来仍然很酷?

1 个答案:

答案 0 :(得分:2)

要为同一路径定义不同的方法(GET / PUT / DELETE / etc。),只需在此路径下列出这些方法,如下所示:

paths:
  /students/{studentId}:
    # Common parameter for all methods on this path
    parameters:
      - name: studentId
        in: path
        ...

    get:
      summary: Get a student by ID
      ...

    put:
      summary: Update a student
      ...

    delete:
      summary: Delete a student
      ...