SpringBoot整合Swagger2

在这里插入图片描述

  • 前言

现在都奉行前后端分离开发和微服务大行其道,分微服务及前后端分离后,前后端开发的沟通成本就增加了。所以一款强大的RESTful API文档就至关重要了。而目前在后端领域,基本上是Swagger的天下了。

  • Swagger2介绍

Swagger是一款RESTful接口的文档在线自动生成、功能测试功能框架。一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务,加上swagger-ui,可以有很好的呈现。

  • SpringBoot集成

  • 配置pom文件

    <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>
    
  • 创建SwaggerProperties

    @Data
    @ConfigurationProperties(prefix = "swagger")
    public class SwaggerProperties {
        private String title;
        private String description;
        private String version;
        private String license;
        private String licenseUrl;
        private String contactName;
        private String contactUrl;
        private String contactEmail;
    }
    
  • 创建BootAdminCoreConfig

    @Configuration
    @EnableConfigurationProperties({SwaggerProperties.class})
    public class BootAdminCoreConfig {
    }
    
  • 配置application.yml

    swagger:
        title: "Boot Admin API"
        description: "Boot Admin 接口文档说明"
        version: "2.0"
        license: "Apache License 2.0"
        licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0"
        contactName: "boot admin"
        contactUrl: "http://blog.qiuyd.com"
        contactEmail: "abelethan@126.com"
    
  • 创建Swagger2Config

    @Configuration
    @EnableSwagger2
    public class Swagger2Config {
    
        @Autowired
        private SwaggerProperties swagger;
    
        @Bean
        public Docket createRestApi(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title(swagger.getTitle())
                    .description(swagger.getDescription())
                    .license(swagger.getLicense())
                    .licenseUrl(swagger.getLicenseUrl())
                    .contact(new Contact(swagger.getContactName(), swagger.getContactUrl(), swagger.getContactEmail()))
                    .version(swagger.getVersion())
                    .build();
        }
    }
    
  • 接口配置

    @Api(value = "用户API", tags = {"用户操作接口"})
    public interface UsersApi {
    
        /**
         * 获取用户信息
         *
         * @return
         */
        @ApiOperation(value = "获取用户信息", notes = "用户详细信息,附带角色信息,权限信息", httpMethod = "GET")
        @GetMapping("/info")
        Wrapper<UserInfoDTO> getInfo();
    
        /**
         * 获取用户信息 分页查询
         *
         * @param query
         * @return
         */
        @ApiOperation(value = "获取用户信息 分页查询", notes = "用户信息分页查询", httpMethod = "GET")
        @ApiImplicitParam(name = "query", value = "用户信息查询条件", required = true, dataType = "UserVoQuery")
        @GetMapping("/page")
        Wrapper<UserVoQuery> pageByQuery(UserVoQuery query);
    
        /**
         * 增加用户信息 带绑定信息
         *
         * @param userVO
         * @return
         */
        @ApiOperation(value = "增加用户信息", notes = "增加用户信息 待绑定信息", httpMethod = "POST")
        @ApiImplicitParam(name = "userVO", value = "用户信息", required = true, dataType = "UserVO")
        @PostMapping
        Wrapper<Boolean> save(@RequestBody UserVO userVO);
    
        /**
         * 修改用户信息 待绑定信息
         *
         * @param userVO
         * @return
         */
        @ApiOperation(value = "修改用户信息", notes = "修改用户信息 待绑定信息", httpMethod = "PUT")
        @ApiImplicitParam(name = "userVO", value = "用户信息", required = true, dataType = "UserVO")
        @PutMapping
        Wrapper<Boolean> update(@RequestBody UserVO userVO);
    
        /**
         * 删除用户信息
         *
         * @param id
         * @return
         */
        @ApiOperation(value = "删除用户信息", notes = "删除用户信息", httpMethod = "DELETE")
        @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
        @DeleteMapping("/id/{id}")
        Wrapper<Boolean> delete(@PathVariable("id") Long id);
    
        /**
         * 主键查询用户信息
         *
         * @param id
         * @return
         */
        @ApiOperation(value = "主键查询用户信息", notes = "查询用户信息", httpMethod = "GET")
        @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
        @GetMapping("/id/{id}")
        Wrapper<Users> get(@PathVariable("id") Long id);
    }
    
  • Swagger访问与使用

    api首页路径:http://127.0.0.1:8080/swagger-ui.html
    在这里插入图片描述
    调试:点击需要访问的api列表,点击try it out!按钮,即可弹出一下页面:
    在这里插入图片描述
    执行
    在这里插入图片描述

常用的注解@Api@ApiOperation@ApiModel@ApiModelProperty示例中有进行标注,对于其他注解,大家可自动谷歌。
其它相关的操作可自行查看官网文档

  • 总结

本文主要是对Swagger的集成和简单使用进行了说明,详细的用法,可自行搜索相关资料下,这里就不阐述了。最后,强烈建议在生产环境关闭swagger,避免不必要的漏洞暴露!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AbelEthan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值