02-Swagger基本介绍以及在谷粒学院整合swagger

Swagger基本介绍以及在谷粒学院整合swagger

一.基本概念

前后端分离开发模式中,api文档是最好的沟通方式。

二.特点

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。

  1. 及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)

  2. 规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)

  3. 一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)

  4. 可测性 (直接在接口文档上进行测试,以方便理解业务)

三.工程中整合swagger

1.在common模块中引入依赖

<dependencies>
        <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>
        <!--lombok用来简化实体类:需要安装lombok插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</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>
        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
<!--         spring2.X集成redis所需common-pool2-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.6.0</version>
        </dependency>
    </dependencies>

2.创建service_base模块

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0XWllafQ-1629774302930)(C:\Users\郭俊林\AppData\Roaming\Typora\typora-user-images\image-20210824105813393.png)]

3.在service_base模块创建swagger的配置类

package com.sunset.servicebase;

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//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("Helen", "http://atguigu.com",
                        "55317332@qq.com"))
                .build();
    }
}

4.在service模块引入service_base,做到模块之间的调用

        <dependency>
            <groupId>com.sunset</groupId>
            <artifactId>service_base</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

5.添加包扫描注解

@ComponentScan(basePackages = {"com.sunset"})

6.测试controller

启动项目后固定地址:主机名:当前模块的端口号/swagger-ui.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 首先,需要在 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 接着,在 Spring Boot 的启动类上添加 `@EnableSwagger2WebFlux` 注解: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux; @SpringBootApplication @EnableSwagger2WebFlux public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 最后,在你的控制器类上添加 `@Api` 和 `@ApiOperation` 注解进行 API 文档的描述: ```java import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") @Api(tags = "HelloController", description = "示例控制器") public class HelloController { @GetMapping("/") @ApiOperation(value = "hello", notes = "示例接口") public String hello() { return "Hello, Swagger!"; } } ``` 然后,启动应用程序并访问 `http://localhost:<port>/swagger-ui/index.html` 即可查看生成的 API 文档。 ### 回答2: Spring Boot整合Springfox Swagger 3.0 Springfox Swagger是一个用于为Spring Boot应用程序生成文档的框架。在Spring Boot中使用Springfox Swagger 3.0,可以方便地为API生成可视化的接口文档,并提供简化的API调试和测试功能。下面是整合Springfox Swagger 3.0的步骤: 首先,需要在Spring Boot项目的pom.xml文件中添加Springfox Swagger 3.0的依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 然后,在Spring Boot应用程序的启动类上添加`@EnableOpenApi`注解,以启用Springfox Swagger 3.0的功能: ```java @SpringBootApplication @EnableOpenApi public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 接下来,需要在项目中添加一个配置类,其中配置Swagger的相关信息: ```java @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 上述配置中,通过`apis`方法指定需要生成文档的Controller所在的包,通过`paths`方法指定需要生成文档的接口路径。可以根据需要进行自定义配置。 最后,在浏览器中访问http://localhost:8080/swagger-ui/index.html,可以看到生成的接口文档页面。在该页面上,可以查看API的详细信息,进行测试和调试。 整合Springfox Swagger 3.0是一个方便快捷的方式来创建和管理API文档。它提供了友好的UI界面和强大的功能,可以大大简化API文档的维护工作,并提高团队合作效率。 ### 回答3: Spring Boot整合Springfox Swagger 3.0的步骤如下: 1. 添加相关依赖:在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 2. 创建Swagger配置类:创建一个配置类,并使用`@Configuration`注解进行标记。在该类中,可以配置Swagger的相关信息,如标题、描述、版本等。 ```java @Configuration @EnableSwagger2 //启用Swagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API文档标题") .description("API文档描述") .version("1.0.0") .build(); } } ``` 3. 配置Swagger的URL路径:在`application.properties`或`application.yml`文件中,添加以下配置项,指定Swagger的URL路径: ```yaml springfox.documentation.swagger-ui.path=/swagger-ui.html ``` 4. 启动项目:启动Spring Boot项目,访问http://localhost:8080/swagger-ui.html,即可查看生成的API文档。 以上是Spring Boot整合Springfox Swagger 3.0的基本步骤,通过配置Swagger相关信息,可以实现自动生成API文档,并提供可视化的界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值