Simple instructions for using Swagger

Reference article:
https://zhuanlan.zhihu.com/p/161947638
https://mp.weixin.qq.com/s?__biz=Mzg2NTAzMTExNg==&mid=2247483909&idx=1&sn=201ee629b9ce3b9276a263e18812e607&scene=19#wechat_redirect


1. Dependency Import

1.1 Direct import

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

1.2 Dependency based on SpringBoot

<dependency>
     <groupId>io.springfox</groupId>
      <artifactId>springfox-boot-starter</artifactId>
      <version>3.0.0</version>
</dependency>

After Swagger reached 3.0, it has been integrated into SpringBoot, and only one dependency needs to be imported.


2. Configuration

2.1 Swagger2 configuration

Create a SwaggerConfig.class and add the annotation @EnableSwagger2 to its class to use it. If you are using SpringBoot, you can also add the annotation to the Application class. The same is true below.

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    

    @Bean
    public Docket docket() {
    
    
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mei.Controller"))
                .paths(PathSelectors.any()) //选择要扫描的路径
                .build();
    }

    private ApiInfo apiInfo() {
    
    
        //作者信息
        Contact contact = new Contact("Xavier", null, "[email protected]");

        return new ApiInfo(
                "学习Swagger的使用",
                "如何使用Swagger",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}

Explanation:
    The apis() in Docket is to select which packages can generate api information, and paths() is to select the paths that need to be scanned.


2.2 Swagger3 configuration

Replace @EnableSwagger2 annotation with @EnableOpenApi annotation



3. Use

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


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


@ApiModel:用在实体类(pojo)上,说明实体类的信息
    @ApiModelProperty:用在实体类的属性上,描述属性的用途

--------------------------------------------------------------------------------

@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:抛出异常的类

4. Examples

User.class:

@ApiModel("用户实体")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    
    
    private Integer id;
    
    @ApiModelProperty("用户名")
    private String name;
    
    private String pwd;
}

MybatisController.class:

@Api(tags = "使用MyBatis进行CRUD")
@RestController
@RequestMapping("/mybatis")
public class MybatisController {
    
    

    @Autowired
    private UserMapper userMapper;

    @ApiOperation("查询所有用户")
    @GetMapping("/queryUserList")
    public List<User> queryUserList() {
    
    
        return userMapper.queryUserList();
    }

    @ApiOperation("用id查询用户")
    @GetMapping("/queryUserById/{id}")
    public User queryUserById(@PathVariable("id") int id) {
    
    
        return userMapper.queryUserById(id);
    }

    @ApiOperation("增加用户")
    @GetMapping("/add")
    public String addUser() {
    
    
        userMapper.addUser(new User(1, "xxxx", "123456"));
        return "add_OK";
    }

    @ApiOperation("更新用户")
    @GetMapping("/update")
    public String updateUser() {
    
    
        userMapper.updateUser(new User(8, "xxx", "789456"));
        return "update_OK";
    }

    @ApiOperation("使用id删除用户")
    @GetMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") int id) {
    
    
        userMapper.deleteUser(id);
        return "delete_OK";
    }
}

effect:
Insert picture description here

Guess you like

Origin blog.csdn.net/lendsomething/article/details/113947217