springboot整合swagger

一、springboot整合swagger

1.创建maven或springboot项目

2.添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>provided </scope>
        </dependency>

        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <scope>provided </scope>
        </dependency>

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <scope>provided </scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <scope>provided </scope>
        </dependency>
注意:版本号自行选择

3.创建配置类

@Configuration//配置类
@EnableSwagger2 //swagger注解
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){
        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("java", "https://www.cnblogs.com/nlbz/", "sxs_0671@163.com"))
                .build();
    }
}

4.在所需位置的启动类上添加扫描

@ComponentScan(basePackages = {"com.xxx"})//扫描到配置类上层或其他层的包下

5.swagger地址以及常用依赖

1.访问地址:

http://localhost:8001/swagger-ui.html //端口号自行更改

2. 常用依赖

@Api(tags = "讲师管理") 作用是在使用swagger时给controller添加中文提示(可不写)
@ApiParam(name = "id",value = "讲师id",required = true) 作用是使用swagger时给参数添加中文提示(可不写)
@ApiOperation(value = "所有讲师数据") 作用是使用swagger时给方法添加中文提示(可不写)
@ApiOperation (value = "接口说明", httpMethod = "接口请求方式", response ="接口返回参数类型", notes = "接口发布说明")
posted @ 2021-08-08 15:40  __先森  阅读(193)  评论(0编辑  收藏  举报