SpringBoot集成Swagger3--在线API文档(详细步骤+图解)

7,372 阅读3分钟

Open API

OpenApi是业界真正的 api 文档标准,其是由 Swagger 来维护的,并被linux列为api标准,从而成为行业标准。  

Swagger

swagger 是一个 api 文档维护组织,后来成为了 Open API 标准的主要定义者,现在最新的版本为17年发布的 Swagger3(Open Api3)。 国内绝大部分人还在用过时的swagger2(17年停止维护并更名为swagger3) swagger2的包名为 io.swagger,而swagger3的包名为 io.swagger.core.v3。 

本文将通过几个简单步骤教大家如何集成Swagger3,废话不多说,直接上步骤。

  • 在项目的pom文件中加入Swagger3依赖 

        <!--Swagger3-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
    
  • 在SpringBoot启动类上添加@EnableOpenApi注解开启Swagger3 

    @SpringBootApplication @EnableOpenApi public class DemoAllApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoAllApplication.class, args);
    }
    

    }

  • 添加Swagger3自定义配置类(可选) 

    @Configuration public class Swagger3Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger3接口文档")
                .description("欢迎关注公众号:撸码社区")
                .contact(new Contact("撸码社区", "http://localhost:8080/swagger-ui/index.html", "ctw17680251029@163.com"))
                .version("1.0")
                .build();
    }
    

    }

  • 在控制器添加Swagger相关注解 

    @Api(tags = "控制器--Hello") @RequestMapping("/hello") @RestController public class HelloController {

    @PostMapping("/insert")
    @ApiOperation("新增方法--insert")
    public String insert() {
        return "insert";
    }
    
    @PutMapping("/update")
    @ApiOperation("修改方法--update")
    public String update() {
        return "update";
    }
    
    @DeleteMapping("/delete")
    @ApiOperation("删除方法--delete")
    public String delete() {
        return "delete";
    }
    
    @GetMapping("/getAll")
    @ApiOperation("查询方法--getAll")
    public String getAll() {
        return "getAll";
    }
    

    }

  • 启动项目,访问:http://localhost:8080/swagger-ui/index.html 

在这里插入图片描述

  • 添加bootstrap-ui支持,添加swagger-bootstrap-ui依赖(可选) 

        <!--swagger-bootstrap-ui-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
    
  • 启动项目,访问:http://localhost:8080/doc.html (可选) 

在这里插入图片描述

相关注解:

@Api:用在请求的类上,表示对类的说明
    tags="说明该类的作用,可以在UI界面上看到的注解"
    value="该参数没什么意义,在UI界面上也看到,所以不需要配置"

@ApiOperation:用在请求的方法上,说明方法的用途、作用
    value="说明方法的用途、作用"
    notes="方法的备注说明"

@ApiImplicitParams:用在请求的方法上,表示一组参数说明
    @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
        name:参数名
        value:参数的汉字说明、解释
        required:参数是否必须传
        paramType:参数放在哪个地方
            · header --> 请求参数的获取:@RequestHeader
            · query --> 请求参数的获取:@RequestParam
            · path(用于restful接口)--> 请求参数的获取:@PathVariable
            · div(不常用)
            · form(不常用)    
        dataType:参数类型,默认String,其它值dataType="Integer"       
        defaultValue:参数的默认值

@ApiResponses:用在请求的方法上,表示一组响应
    @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
        code:数字,例如400
        message:信息,例如"请求参数没填好"
        response:抛出异常的类

@ApiModel:用于响应类上,表示一个返回响应数据的信息
            (这种一般用在post创建的时候,使用@RequestBody这样的场景,
            请求参数无法使用@ApiImplicitParam注解进行描述的时候)
    @ApiModelProperty:用在属性上,描述响应类的属性

以上就是全部步骤了!非常的简单实用!希望大家喜欢!

有什么疑问可评论区提问! 求点赞!求转发!求收藏!