SpringBoot整合Swagger2

1. Swagger依赖


    io.springfox
    springfox-swagger-ui
    2.9.2


    io.springfox
    springfox-swagger2
    2.9.2

SpringBoot 整合Swagger2 踩坑记录

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

报错的原因是由于SpringBoot的版本跟Swagger的版本有冲突。
SpringBoot用的2.6.1的版本,Swagger用的2.9.2的版本;最后降低了SpringBoot的版本为2.5.6解决了问题。

2. Swagger配置文件

package com.skyi.security.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;

/**
 * @Classname SwaggerConfig Swagger2配置类
 * @Description TODO
 * @Version 1.0.0
 * @Date 2021/12/7 9:03
 * @Created by pts
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否开启 (true 开启  false隐藏。生产环境建议隐藏)
                //.enable(false)
                .select()
                //扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
                .apis(RequestHandlerSelectors.basePackage("com.skyi.security.controller"))
                //指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //设置文档标题(API名称)
                .title("SpringBoot中使用Swagger2接口规范")
                //文档描述
                .description("接口说明")
                //服务条款URL
                .termsOfServiceUrl("http://localhost:8086/")
                //版本号
                .version("1.0.0")
                .build();
    }
}

3. 测试控制器

package com.skyi.security.controller;


import com.skyi.security.common.Result;
import com.skyi.security.entity.User;
import com.skyi.security.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 

* 前端控制器 *

* * @author pts * @since 2021-12-06 */ @RestController @RequestMapping("/user/") @CrossOrigin @Api(value = "测试接口", tags = "用户管理相关的接口", description = "用户测试接口") public class UserController { @Autowired private UserService userService; @PostMapping("register") //方法参数说明,name参数名;value参数说明,备注;dataType参数类型;required 是否必传;defaultValue 默认值 @ApiImplicitParam(name = "user", value = "新增用户数据",dataType = "User",required = true) //说明是什么方法(可以理解为方法注释) @ApiOperation(value = "添加用户", notes = "添加用户") public Result register(@RequestBody User user){ List list = userService.lambdaQuery().eq(User::getUsername, user.getUsername()).list(); if (list.size()>0){ return Result.failed("用户已存在!"); } boolean flag = userService.save(user); if (flag) return Result.succeed("注册成功!"); else return Result.failed("注册失败!"); } }

4. Swagger页面访问

image.png

5. Swagger注解详解

Springboot优雅集成Swagger2 - 云+社区 - 腾讯云 (tencent.com)

你可能感兴趣的:(SpringBoot整合Swagger2)