Spring Boot 中自定义swagger-ui的URL(path)和样式(UI)

本文介绍了如何在SpringBoot中自定义Swagger-ui,包括将Swagger-ui的默认页面替换为自定义HTML,设置自定义URL,并展示如何修改UI样式,如添加Logo、更改头部颜色等。通过在resources/swagger文件夹下放置下载的dist文件,并在application.yml及WebMvcConfigurer中配置,可以实现Swagger文档页面的个性化定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:

Spring Boot写的API需要用到的Swagger-ui.html API文档页面,但自动生成的页面,会包含swagger等信息,不是很友好,希望增加我们自己的Logo,以及修改UI style。


解决:

这里不再重复如何配置swagger,网上已经很多教程。

下面是设置自定义HTML和自定义URL(以/v1/api-docs为例)的教程:

  1. 首先到github的swagger-ui 项目中下载dist文件夹里面的所有文件(https://github.com/swagger-api/swagger-ui/tree/master/dist),放到spring-boot的resources/swagger/文件夹中。
    在这里插入图片描述
  2. 在appilication.yml文件中增加如下配置
springfox:
  documentation:
    swagger:
      v2:
        path: /v1/api-docs
  1. 再在WebMvcConfigurer文件中加入映射的相关配置:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //关闭原生swagger-ui, 自动redirect去根目录
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/");
        //新的api document ui
        registry.addResourceHandler("/v1/api-docs/**").addResourceLocations("classpath:/swagger/");
    }
}
  1. SwaggerConfig.java配置无需特别修改,以下是我自己的参考:
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.enable}")
    private boolean enableSwagger;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                // default show base information
                .apiInfo(apiInfo())
                // Set which interfaces are exposed to Swagger for display
                .select()
                // Scan all annotated APIs
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

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

}
  1. 最后打开https://localhost/v1/api-docs/index.html,即可看到最新的UI,想要修改样式到resource/swagger中修改相关的JS和CSS文件即可!(我自己就加了Logo,删了搜索库,改了header为白色(如下图,原谅打码))

在这里插入图片描述

Spring Boot集成Swagger-UI主要包括以下几个步骤: 1. **添加依赖**:首先,在项目的pom.xml或build.gradle文件中添加Swagger-UI的依赖。例如对于Springfox,你可以添加以下Maven依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>最新版本号</version> </dependency> ``` 或者Gradle: ```groovy implementation 'io.springfox:springfox-swagger-ui' ``` 2. **启用Springfox**:在@Configuration类上添加`@EnableSwagger2WebMvc`注解开启Swagger功能。 ```java @SpringBootApplication @EnableSwagger2WebMvc public class Application { //... } ``` 3. **配置Swagger**:在application.properties或application.yml中设置Swagger的基本信息,如标题、描述默认API路径: ```properties swagger.enabled=true springfox.documentation.path=/api-docs springfox.documentation.apiInfo.title=Your API Title springfox.documentation.apiInfo.description=Your API Description ``` 4. **扫描API接口**:使用`@RestController`、`@RequestMapping``@ApiOperation`等SpringFox注解来标记需要包含在文档中的ControllerAPI方法。 5. **整合前端展示**:为了直接访问Swagger UI,你需要配置一个端点指向Swagger资源。通常这涉及到创建一个"/v2/api-docs"的映射,供Swagger自动发现并处理。你也可以选择将其部署在一个静态目录,并配置Spring MVC的静态资源处理器去处理。 6. **启动应用**:最后,运行Spring Boot应用,通过指定的Swagger URL(如`http://localhost:8080/swagger-ui.html`)就可以查看生成的API文档了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值