不#34;试一试" Swagger UI中HEAD方法的按钮

时间:2017-03-20 12:52:10

标签: swagger swagger-ui swagger-2.0

我有一个定义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?

HEAD operation in Swagger UI

3 个答案:

答案 0 :(得分:4)

您可以尝试Swagger UI 3.0 - HEAD已经尝试过#34;默认情况下,此版本。

如果您使用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

答案 1 :(得分:0)

要添加,在Java中,您可以在swagger config类中添加bean来做到这一点:

@Configuration
@EnableSwagger2
public class SwaggerConfig { 

    @Bean
    UiConfiguration uiConfig()
    {
      return new UiConfiguration( null, UiConfiguration.Constants.NO_SUBMIT_METHODS );
    }
}

基本上它将supportedSubmitMethods放到[]

答案 2 :(得分:0)

您需要通过“ head”值扩展DEFAULT_SUBMIT_METHODS。

    @EnableSwagger2
    @Configuration
    public class SwaggerConfig  {

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