SpringBoot集成swagger接口开发

1.在自己的项目pom文件里面必须要添加的两个依赖

<!--  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>

2.添加配置文件,在配置类里面添加
@Configuration
@EnableSwagger2
其中@Configuration表示这个类是一个配置文件,@EnableSwagger2表示集成swagger接口,在配置文件里面添加以下内容:

@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yzj.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("SpringBoot整合Swagger")
                        .description("SpringBoot整合Swagger,详细信息......")
                        .version("9.0")
                        .license("The Apache License")
                        .licenseUrl("http://www.baidu.com")
                        .build());
    }

关键点:

.apis(RequestHandlerSelectors.basePackage("com.yzj.controller"))

这是要扫描的controller文件,

 .title("SpringBoot整合Swagger")
                        .description("SpringBoot整合Swagger,详细信息......")
                        .version("9.0")
//                        .contact(new Contact("啊啊啊啊","blog.csdn.net","[email protected]"))
                        .license("The Apache License")
                        .licenseUrl("http://www.baidu.com")
                        .build());

这些都是swagger文件的描述信息,不太重要吗,主要是自己看,能看懂都行,只要在controller已经配置好了get,post,put等接口,直接在浏览器里面输入url:http://localhost:8080/swagger-ui.html#/即可展示页面如下.
!](https://img-blog.csdnimg.cn/20210115154633659.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMTQ3NTkx,size_16,color_FFFFFF,t_70)
这些都是自己写的接口,可以直接在里面测试
可以直接进行测试,返回json串

猜你喜欢

转载自blog.csdn.net/qq_43147591/article/details/112670469