首页 文章

使用Springfox更改Swagger UI的 Headers 和描述

提问于
浏览
1

我正在构建一个Spring Boot应用程序,并使用Spring Fox Swagger UI使用Swagger UI对其进行记录 . 我've got everything documented, but want to customize the title and description but can'弄清楚如何 . 例如,在此图像中:https://springfox.github.io/springfox/docs/current/images/springfox-swagger-ui.png Headers 为"Springfox petstore API",描述为Lorem Ipsum . 但在我的Swagger UI中, Headers 和描述都说"API Documentation" . 我尝试过使用@SwaggerDefinition注释,但似乎没有做任何事情 .

1 回答

  • 1

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

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

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

    从代码中,检查Swagger编辑器生成的类,并将其与您的代码进行比较 . 您可以设置描述和 Headers ,在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,我可以更好地帮助您 .

    祝贺你!

相关问题