前言技术之Swagger

目录

一、Swagger介绍

1、定义:

2、为什么会用到Swagger

3、Swagger作用

4、Swagger优点

二、集成Swagger

       1、新建springboot项目

       2、集成swagger 

                2.1. 打开https://mvnrepository.com/ , 查找springbox,在pom.xml中导入如下图标出的依赖。

                2.2. 导入依赖

                2.3. 编写swagger的配置类

       3、开发一个controller用于测试

       4、启动服务,验证集成效果

三、Swagger常用注解

        1、常用注解如下


一、Swagger介绍

1、定义:

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。

2、为什么会用到Swagger

前后端分离是的前端与后端之间的职责更加明确后台: 负责业务处理前端: 负责显示逻辑在这种情况下,前端和后端可以分别交付给专业的开发人员去做,所以是必须要定义前后端直接的对接接口,否则各自为是则项目无法集成,这时就需要一个文档来定义统一的接口。

但是:在没有swagger之前

我们可以使用word,excel等功能来书写接口定义文档,但又有一个弊端,即:在接口发送改变时需要及时的同步接口文档,否则实际的接口与接口文档不相符,则接口文件就失去了作用,甚至会起到反作用。所以就诞生了Swagger

3、Swagger作用

根据在代码中使用自定义的注解来生成接口文档,这个在前后端分离的项目中很重要。这样做的好处是在开发接口时可以通过swagger将接口文档定义好,同时也方便以后的维护。

        

4、Swagger优点

根据在代码中使用自定义的注解来生成接口文档,这个在前后端分离的项目中很重要。这样做的好处是在开发接口时可以通过swagger将接口文档定义好,同时也方便以后的维护。

二、集成Swagger

       1、新建springboot项目

       2、集成swagger 

                2.1. 打开https://mvnrepository.com/ , 查找springbox,在pom.xml中导入如下图标出的依赖。

前一俩个依赖都需要: 

                2.2. 导入依赖

        <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.3. 编写swagger的配置类

package com.example.swagger01.config;

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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SwaggerDemo API DOC")
                .description("SwaggerDemo API DOC")
                .version("1.0")
                .termsOfServiceUrl("https://www.baidu.com")
                .build();
    }


}

注意: 该配置类需要根据自己的项目修改,如以下配置paths 指定需要生成文档的url规则title 文档标题description 描述

       3、开发一个controller用于测试

package com.example.swagger01.controller;

import com.example.swagger01.pojo.User;
import com.example.swagger01.response.ResponseResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/swaggerdemo")
@Api(tags = "控制类")
public class HelloController {

    @ApiOperation(value = "欢迎信息")
    @GetMapping("/hello")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "msg", value = "消息", dataType = "string", paramType = "query", required = true)
    })
    public ResponseResult<?> hello(String name, String msg) {
        return ResponseResult.success();
    }

    @PostMapping("/register")
    @ApiOperation("注册用户接口")
    @ApiResponses({
            @ApiResponse(code = 10001, message = "Exception 01"),
            @ApiResponse(code = 10002, message = "Exception 02"),
            @ApiResponse(code = 10003, message = "Exception 03")
    })
    public ResponseResult<?> register(@RequestBody User user) {
        return ResponseResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation("修改用户信息")
    public ResponseResult<?> edit(User user) {
        return ResponseResult.success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", dataType = "string", paramType = "path", required = true)
    public ResponseResult<?> delete(@PathVariable("id") String id) {
        return ResponseResult.success();
    }


}

       4、启动服务,验证集成效果

服务启动后,访问:http://localhost:8080/swagger-ui.html

三、Swagger常用注解

        1、常用注解如下

 

 

header 请求参数的获取:@RequestHeader

query  请求参数的获取:@RequestParam

path(用于restful接口) 请求参数的获取:@PathVariable

body(不常用)

form(不常用) 

 defaultValue="参数的默认值"required="true" 表示参数是否必须传 |

 因为swagger是一个在线生成接口文档,所以我们可以进行测试


这个注解是在类上面进行注释的,可以说明这个类是什么类

 

测试完了类,接下来测试实体类的属性

 进行发送请求:

 发送成功!

今天的知识就分享到这了,希望能够帮助到你!

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值