Spring Boot中的Swagger UI定制

发布时间:2024-10-05 16:33:03 作者:小樊
来源:亿速云 阅读:81

在Spring Boot项目中使用Swagger UI时,可以通过以下步骤进行定制:

  1. 引入依赖: 确保你的pom.xml文件中已经引入了Swagger的相关依赖。例如:
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
  1. 创建Swagger配置类: 创建一个配置类,例如SwaggerConfig.java,并使用@EnableSwagger2注解来启用Swagger。在这个类中,你可以定义API的基本信息、描述、版本等,以及指定API的扫描路径。
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("My API")
                .description("My API description")
                .version("1.0")
                .build();
    }
}
  1. 定制Swagger UI: 在SwaggerConfig.java中,你可以通过Docket对象来自定义Swagger UI的显示。例如,你可以更改主题颜色、添加图标、设置页面标题等。
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .globalOperationParameters(getGlobalOperationParameters())
                .pathMapping("/v1"); // 自定义API的基本路径
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("My API")
                .description("My API description")
                .version("1.0")
                .build();
    }

    private List<GlobalOperationParameter> getGlobalOperationParameters() {
        return Arrays.asList(
                new GlobalOperationParameterBuilder()
                        .name("token")
                        .description("用户认证token")
                        .modelRef(new ModelRef("string"))
                        .parameterType("header")
                        .required(false)
                        .build()
        );
    }
}

此外,你还可以通过访问http://localhost:8080/swagger-ui.html(假设你的应用运行在8080端口)来查看和定制Swagger UI。在这个页面上,你可以找到很多用于定制UI的选项,例如更改主题颜色、添加图标等。

注意:以上示例代码是基于Spring Boot 2.x和Swagger 2.9.2的,如果你使用的是其他版本,可能需要相应地调整代码。

推荐阅读:
  1. spring boot中怎么整合swagger-ui
  2. springboot中swagger快速启动流程

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot

上一篇:Spring Boot与JWT刷新令牌机制

下一篇:Spring Boot与Spring Cloud Gateway路由配置

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》