从零搭建SpringBoot后台框架(五)——集成Swagger

180 阅读1分钟

一、什么是Swagger

Swagger是一款通过我们添加的注解来对方法进行说明,来自动生成项目的在线api接口文档的web服务。

二、添加Swagger2依赖

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

说明:swagger-models在tag里面没有,后续解决报错添加

修改application.yml添加配置,解决启动失败问题

spring:
  mvc:
    pathmatch:
      #Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X+ 使用的是PathPatternMatcher
      matching-strategy: ant_path_matcher

三、创建Swagger2配置文件

package com.example.demo.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;

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //配置api扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot示例")
                .version("V1.0.0")
                .build();
    }
}

四、修改Controller,添加API注解

package com.example.demo.controller;

import com.example.demo.common.http.BusinessException;
import com.example.demo.common.http.RestResult;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("userInfo")
@Api(tags = "用户信息")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/selectById")
    @ApiOperation(value = "根据id查询用户信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
    )
    public RestResult<UserInfo> selectById(Long id) {
        UserInfo userInfo = userInfoService.selectById(id);
        if (null == userInfo) {
            throw new BusinessException("用户不存在");
        }
        return RestResult.getSuccessResult(userInfo);
    }

    @PostMapping("/testException")
    public RestResult<UserInfo> testException(Long id) {
        UserInfo userInfo = null;
        userInfo.getId();
        return RestResult.getSuccessResult();
    }
}

五、接口测试

访问:http://localhost:8080/swagger-ui/index.html 可以看到下面页面

image.png

打开POST /userInfo/selectById,点击try it out,输入参数点击执行,可以看到接口返回参数

image.png

六、项目地址

gitee

PS:可以通过tag下载本文对应的代码版本

七、结尾

swagger集成已完成,有问题可以联系chenzhenlindx@qq.com

八、参考文章

  1. 从零搭建自己的SpringBoot后台框架(六)
  2. swgger官网文档:docs.swagger.io/