如何在 Spring 引导和 Swagger 2.0 中自定义 tom-level 属性?

How to customize tom-level properties in Spring boot and Swagger 2.0?

有人知道如何在 Spring boot 和 Swagger 2.0 中自定义 tom-level 属性吗?

我试过使用@SwaggerDefinition,但这似乎不起作用。我下面的代码有没有错误?

@SpringBootApplication
@ComponentScan(basePackages = { "test" })
@EnableSwagger2
@SwaggerDefinition(info = @Info(title = "My Api Documentation",
  description = "My Api Documentation, Version:1.0",
  version = "1.0",
  contact = @Contact(name = "my name", email = "my_name@gmail.com", url = "http://my_page/") ,
  license = @License(name = "Apache 2.0", url = "http://www.apache.org/licenses/LICENSE-2.0") ) )
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

我从 http://localhost:8080/v2/api-docs

得到了以下 json 回复
{
swagger: "2.0",
info: {
description: "Api Documentation",
version: "1.0",
title: "Api Documentation",
termsOfService: "urn:tos",
contact: {
name: "Contact Email"
},
license: {
name: "Apache 2.0",
url: "http://www.apache.org/licenses/LICENSE-2.0"
}
},
host: "localhost:8080",
basePath: "/",
tags: [
{
name: "basic-error-controller",
description: "Basic Error Controller"
}
],
...
}

top-level 属性(标题、描述)应该已经更改。

我来回答自己。 我没有解决@SwaggerDefinition的问题,但是我找到了另一种方法。

我可以在我的配置中覆盖 Docket class。

@SpringBootApplication
@ComponentScan(basePackages = { "test" })
@EnableSwagger2
public class Application {

  @Bean
  public Docket confApi() {
    ResponseMessage msg_500 = new ResponseMessageBuilder().code(500).message("500 message").responseModel(new ModelRef("Error")).build();
    return new Docket(DocumentationType.SWAGGER_2).globalResponseMessage(RequestMethod.GET, Collections.singletonList(msg_500))
        .globalResponseMessage(RequestMethod.POST, Collections.singletonList(msg_500))
        .apiInfo(new ApiInfo("My Api Documentation", "My Api Documentation, Version: 1.0", "1.0", null, "my_name@gmail.com", null, null));
  }

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}