使用 Springfox 更改 Swagger UI 的标题和描述

Changing title and description of Swagger UI using Springfox

我正在构建一个 Spring 引导应用程序并使用 Springfox Swagger UI 使用 Swagger UI 对其进行记录。我已经记录了所有内容,但想自定义标题和描述,但不知道如何自定义。例如,在这张图片中:https://springfox.github.io/springfox/docs/current/images/springfox-swagger-ui.png 标题是 "Springfox petstore API",描述是 Lorem Ipsum。但是在我的 Swagger UI 中,标题和描述都说 "API Documentation"。我试过使用 @SwaggerDefinition 注释,但它似乎没有做任何事情。

我建议您使用 swagger editor,然后您可以使用顶部菜单中的 "Generate Server" 选项从 API 文档自动生成 Spring 引导服务器。第一次生成 API 真的很棒。

如果您想从 YAML 进行设置,您必须在 info 部分提供此字段:

info:
  version: "1.0.0"
  title: My API title
  description: "Awesome description"

从代码中,检查 Swagger 编辑器生成的 classes 并将其与您的代码进行比较。您可以设置描述和标题,在 class SwaggerDocumentationConfig.

中的 ApiInfo Builder object 上设置相应的属性

这里有代码:

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2017-10-05T14:03:51.916-03:00")

@EnableSwagger2
@Configuration
public class SwaggerDocumentationConfig {

    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("My API Title")
            .description("Awesome description")
            .license("Apache 2.0")
            .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
            .termsOfServiceUrl("")
            .version("1.0.0")
            .contact(new Contact("", "", "contact@contact.com.uy"))
            .build();
    }

    @Bean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.mypackage.api"))
            .build()
            .apiInfo(apiInfo());
    }
}

如果 none 对您有意义,请向我展示更多您的代码,以了解您如何使用 Springfox,我可以更好地帮助您。

此致问候!