Spring Boot 集成 swagger

1.添加pom

复制代码
        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
        <!--swagger 增强界面-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.8.7</version>
        </dependency>
复制代码

2.配置swagger配置类

复制代码
package com.coding.mall.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI  //第三方swagger增强API注解
public class Swagger2Config {
    //核心对象 Docket

    /**
     * 前端
     * @return
     */
    @Bean
    public Docket webApiConfig() {
        // 过滤掉 /admin 的所有请求
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }


    private ApiInfo webApiInfo() {
        // 建造者模式
        return new ApiInfoBuilder()
                .title("艾编程商城-产品中心API文档")
                .description("产品中心微服务!")
                .version("1.0")
                .contact(new Contact("Coding", "http://icodingedu.com", "coding666@qq.com"))
                .build();
    }

    // 后台管理员的
    @Bean
    public Docket adminApiConfig() {
        // 只要 /admin 的所有请求
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();
    }

    private ApiInfo adminApiInfo() {
        // 建造者模式
        return new ApiInfoBuilder()
                .title("艾编程商城-产品中心管理端API文档")
                .description("产品中心微服务管理中心!")
                .version("1.0")
                .contact(new Contact("Coding", "http://icodingedu.com", "coding666@qq.com"))
                .build();
    }

}
复制代码

3.访问地址

  如未使用增强ui, 则访问路径为:  http://localhost:8080/swagger-ui.html  (默认)

  增强UI 访问路径: http://localhost:8080/doc.html

 

 

官方文档:http://www.xiaominfo.com/swagger-bootstrap-ui/

posted @   紫枫夜羽  阅读(173)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示