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

时间:2017-11-09 19:20:25

标签: spring-boot swagger-ui springfox

我正在构建一个Spring Boot应用程序,并使用Spring Fox Swagger UI使用Swagger UI对其进行记录。我已经记录了所有内容,但想要自定义标题和描述,但无法弄清楚如何。例如,在此图像中:https://springfox.github.io/springfox/docs/current/images/springfox-swagger-ui.png标题为“Springfox petstore API”,描述为Lorem Ipsum。但在我的Swagger UI中,标题和描述都说“API文档”。我尝试过使用@SwaggerDefinition注释,但它似乎没有做任何事情。

1 个答案:

答案 0 :(得分:3)

我建议您使用swagger editor,然后您可以使用"生成服务器"从API文档自动生成Spring Boot服务器。顶部菜单中的选项。这对于第一次生成API非常有用。

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

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

从代码中,检查Swagger编辑器生成的类,并将其与您的代码进行比较。您可以设置描述和标题,在ApiInfo Builder对象上设置相应的属性,该对象位于SwaggerDocumentationConfig类中。

这里有代码:

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

如果这对您没有任何意义,请向我展示一些代码,了解您如何使用Springfox,我可以更好地帮助您。

Bests问候!