• Swagger笔记


    一、导包

    1. <dependency>
    2. <groupId>io.springfoxgroupId>
    3. <artifactId>springfox-swagger2artifactId>
    4. <version>2.9.2version>
    5. dependency>
    6. <dependency>
    7. <groupId>io.springfoxgroupId>
    8. <artifactId>springfox-swagger-uiartifactId>
    9. <version>2.9.2version>
    10. dependency>

    二、编写配置类

    1. import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import springfox.documentation.builders.ApiInfoBuilder;
    5. import springfox.documentation.builders.PathSelectors;
    6. import springfox.documentation.builders.RequestHandlerSelectors;
    7. import springfox.documentation.service.ApiInfo;
    8. import springfox.documentation.service.Contact;
    9. import springfox.documentation.spi.DocumentationType;
    10. import springfox.documentation.spring.web.plugins.Docket;
    11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    12. @Configuration
    13. @EnableSwagger2
    14. @EnableSwaggerBootstrapUI
    15. public class SwaggerConfig {
    16. @Bean
    17. public Docket createRestApi() {
    18. return new Docket(DocumentationType.SWAGGER_2)
    19. .apiInfo(apiInfo())
    20. .select()
    21. .apis(RequestHandlerSelectors.basePackage("com.example.swaggerdemo"))
    22. .paths(PathSelectors.any())
    23. .build();
    24. }
    25. private ApiInfo apiInfo() {
    26. return new ApiInfoBuilder()
    27. // 标题
    28. .title("Spring Boot中使用Swagger2构建RESTful APIs 的标题")
    29. // Swagger文档的版本号
    30. .version("1.0")
    31. // 标题的详细描述
    32. .description("Spring Boot中使用Swagger2构建RESTful APIs 的详细描述")
    33. // 友链,用的少
    34. .termsOfServiceUrl("http://www.baidu.com")
    35. // 联系人信息
    36. .contact(new Contact("蒋劲豪",
    37. "http://www.hao123.com",
    38. "718009739@qq.com"
    39. ))
    40. // 协议,自定义的
    41. .license("Apache 2.0")
    42. // 把协议变成超链接
    43. .licenseUrl("http://www.bilibili.com")
    44. .build();
    45. }
    46. }

    三、接口文档的访问地址

    http://localhost:8080/swagger-ui.html

    四、常见问题

    SpringBoot和Swagger有版本不兼容问题,如果版本不兼容会出现以下异常:

    org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 

    解决办法:

    (一)换兼容的版本

    (二)改配置文件

    1. spring:
    2. mvc:
    3. pathmatch:
    4. matching-strategy: ant_path_matcher

    五、Controller类的案例

    1. import com.example.swaggerdemo.entity.User;
    2. import io.swagger.annotations.*;
    3. import org.springframework.web.bind.annotation.PostMapping;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. @RequestMapping("/userController")
    8. @Api(tags = {"用户接口"})
    9. public class UserController {
    10. @ApiOperation(value = "新增用户接口", notes = "新增用户接口的详细描述")
    11. // 对请求参数进行说明
    12. @ApiImplicitParams({
    13. @ApiImplicitParam(name = "id", value = "用户编号", dataType = "string",
    14. paramType = "query", required = true, defaultValue = "1001"),
    15. // 如果参数在请求头中 paramType = "header"
    16. // 如果参数在路径中 paramType = "path"
    17. // 如果参数在请求体中 paramType = "query"
    18. @ApiImplicitParam(name = "name", value = "用户姓名", dataType = "string",
    19. paramType = "query", required = true, defaultValue = "tom")
    20. })
    21. // 对响应结果进行说明
    22. @ApiResponses({
    23. @ApiResponse(code = 200, message = "success", response = User.class)
    24. })
    25. @PostMapping("/addUser")
    26. public String addUser(User user) {
    27. return "新增用户成功!";
    28. }
    29. }

    六、实体类的案例

    1. package com.example.swaggerdemo.entity;
    2. import io.swagger.annotations.ApiModel;
    3. import io.swagger.annotations.ApiModelProperty;
    4. import lombok.AllArgsConstructor;
    5. import lombok.Data;
    6. import lombok.NoArgsConstructor;
    7. @Data
    8. @NoArgsConstructor
    9. @AllArgsConstructor
    10. @ApiModel(value = "User", description = "用户的实体类")
    11. public class User {
    12. @ApiModelProperty(value = "用户编号")
    13. private String id;
    14. @ApiModelProperty(value = "用户姓名")
    15. private String name;
    16. }

    七、换其他的前端UI

    如果不喜欢默认的UI界面,可以换一个前端UI界面;

    (一)导包

    1. <dependency>
    2. <groupId>com.github.xiaoymingroupId>
    3. <artifactId>swagger-bootstrap-uiartifactId>
    4. <version>1.9.6version>
    5. dependency>

    (二)添加注解

    在SwaggerConfig配置类上加上注解

    @EnableSwaggerBootstrapUI

    (三)接口文档的访问地址

    http://localhost:8080/doc.html

  • 相关阅读:
    Java上传文件大小受限怎么解决
    如何在USGS下载Landsat 8-9影像(2022年版)
    浅谈泰山众筹合约系统开发逻辑技术方案详解(原理解析)
    【JVM】synchronized锁升级的过程
    嵌入式开发学习之--Git管理代码
    基于GPIO子系统编写LED驱动,编写应用程序进行测试设置定时器,5秒钟打印一次hello world
    【C++】类的封装 ① ( 类和对象 | 面向对象三大特征 - 封装 继承 多态 | 类的封装引入 )
    基于Socket的MFC网络编程
    LeetCode 每日一题——655. 输出二叉树
    HTML页面加锁
  • 原文地址:https://blog.csdn.net/m0_58961367/article/details/134481047