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

fai*_*wle 5 swagger-ui spring-boot springfox

我正在构建一个Spring Boot应用程序,并使用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批注,但是它似乎没有任何作用。

Emi*_*tti 5

我建议您使用swagger编辑器,然后可以使用Top Menu中的“ Generate Server”选项从API文档自动生成Spring Boot服务器。第一次生成API真的很棒。

如果要从YAML进行设置,则必须在信息部分提供以下字段:

info:
  version: "1.0.0"
  title: My API title
  description: "Awesome description"
Run Code Online (Sandbox Code Playgroud)

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

这里有代码:

@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());
    }
}
Run Code Online (Sandbox Code Playgroud)

如果这对您都不有意义,请向我显示更多代码,以了解如何使用Springfox,并且我可以为您提供更好的帮助。

祝贺你!