【问题标题】:No "Try it out" button for HEAD method in Swagger UISwagger UI 中的 HEAD 方法没有“试用”按钮
【发布时间】:2017-08-11 18:17:36
【问题描述】:

我有一个定义 HEAD 操作的 Swagger 规范:

head:
  description: show flight exist or not.

  parameters:
    - name: flight_no
      in: path
      type: string
      description: Flight_no
      required: true
  produces:
    - application/json
    - application/xml

  responses:
    200:
      description: success response
      schema:
        type: array
        items:
          $ref: '#/definitions/Data'
    '404':
      description: flight does not exist

在 Swagger UI v. 2 中,此 HEAD 操作没有“试用”按钮。如何为 HEAD 添加“试用”?

【问题讨论】:

    标签: swagger swagger-ui swagger-2.0


    【解决方案1】:

    您需要将 DEFAULT_SUBMIT_METHODS 扩展为“head”值。

        @EnableSwagger2
        @Configuration
        public class SwaggerConfig  {
    
            @Bean
            UiConfiguration uiConfiguration() {
                return new UiConfiguration( null, new String[] {"get", "post", "put", "delete", "patch", "head"} );
            }
        }
    

    【讨论】:

      【解决方案2】:

      要加起来,在 java 中,您可以在 swagger 配置类中添加 bean 来做到这一点:

      @Configuration
      @EnableSwagger2
      public class SwaggerConfig { 
      
          @Bean
          UiConfiguration uiConfig()
          {
            return new UiConfiguration( null, UiConfiguration.Constants.NO_SUBMIT_METHODS );
          }
      }
      

      基本上它会将supportedSubmitMethods 放到[]

      【讨论】:

        【解决方案3】:

        您可以尝试 Swagger UI 3.0 - HEAD 在此版本中默认“试用”。

        如果您使用 Swagger UI 2.0,则默认情况下禁用 HEAD。您需要在 index.html 中的 Swagger UI 初始化代码中的supportedSubmitMethods 列表中显式启用它:

        window.swaggerUi = new SwaggerUi({
          url: url,
          dom_id: "swagger-ui-container",
          supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch', 'head'],
          //                                                                   ^
          // ------------------------------------------------------------------┘
        


        顺便说一句,HEAD 响应中的schema 并不是真正有用,因为 HEAD 不应该返回实际的主体——只有标头。您应该将 200 响应更改为:

        responses:
          200:
            description: success response
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-03-24
          • 2020-03-23
          • 1970-01-01
          • 2022-12-17
          • 2016-01-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多