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

时间:2015-07-20 21:25:17

标签: java spring-boot swagger-2.0

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

我尝试使用@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"
}
],
...
}

顶层属性(标题,描述)应该已经改变。

1 个答案:

答案 0 :(得分:0)

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

我可以在配置类中覆盖Docket。

@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);
  }
}