当前位置: 首页 > news >正文

Swagger笔记

一、导包

<!--引入swagger-->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>
</dependency>
<!--前端的UI界面-->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version>
</dependency>

二、编写配置类

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.example.swaggerdemo")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder()// 标题.title("Spring Boot中使用Swagger2构建RESTful APIs 的标题")// Swagger文档的版本号.version("1.0")// 标题的详细描述.description("Spring Boot中使用Swagger2构建RESTful APIs 的详细描述")// 友链,用的少.termsOfServiceUrl("http://www.baidu.com")// 联系人信息.contact(new Contact("蒋劲豪","http://www.hao123.com","718009739@qq.com"))// 协议,自定义的.license("Apache 2.0")// 把协议变成超链接.licenseUrl("http://www.bilibili.com").build();}
}

三、接口文档的访问地址

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

四、常见问题

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

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

解决办法:

(一)换兼容的版本

(二)改配置文件

spring:mvc:pathmatch:matching-strategy: ant_path_matcher

五、Controller类的案例

import com.example.swaggerdemo.entity.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/userController")
@Api(tags = {"用户接口"})
public class UserController {@ApiOperation(value = "新增用户接口", notes = "新增用户接口的详细描述")// 对请求参数进行说明@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户编号", dataType = "string",paramType = "query", required = true, defaultValue = "1001"),// 如果参数在请求头中 paramType = "header"// 如果参数在路径中 paramType = "path"// 如果参数在请求体中 paramType = "query"@ApiImplicitParam(name = "name", value = "用户姓名", dataType = "string",paramType = "query", required = true, defaultValue = "tom")})// 对响应结果进行说明@ApiResponses({@ApiResponse(code = 200, message = "success", response = User.class)})@PostMapping("/addUser")public String addUser(User user) {return "新增用户成功!";}
}

六、实体类的案例

package com.example.swaggerdemo.entity;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "User", description = "用户的实体类")
public class User {@ApiModelProperty(value = "用户编号")private String id;@ApiModelProperty(value = "用户姓名")private String name;
}

七、换其他的前端UI

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

(一)导包

<!--新UI-->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.6</version>
</dependency>

(二)添加注解

在SwaggerConfig配置类上加上注解

@EnableSwaggerBootstrapUI

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

http://localhost:8080/doc.html

http://www.lryc.cn/news/236343.html

相关文章:

  • 数据结构 堆
  • 将 ONLYOFFICE 文档编辑器与 Node.js 应用集成
  • CentOS 7搭建Gitlab流程
  • Idea安装完成配置
  • 超详细~25考研规划~感恩现在努力的你!!!
  • 智慧城市安全监控的新利器
  • 【算法】石子合并(区间dp)
  • C++-特殊类和单例模式
  • 【开源】基于Vue.js的智能教学资源库系统
  • C语言之qsort()函数的模拟实现
  • 数字化未来:实时云渲染在智慧城市中的创新应用
  • Go语言常用命令详解(二)
  • ChatGPT 从零到一打造私人智能英语学习助手
  • 算法升级之路(七)-盛最多水的容器
  • milvus数据库索引管理
  • JVM中的 -Xms参数 设置 JVM 的初始堆大小
  • Idea 创建 Spring 项目(保姆级)
  • C++多线程学习(一):C++11 多线程快速入门
  • Linux系统之lsof命令的基本使用
  • 性能压力测试的优势与重要性
  • AtCoder Beginner Contest 329 题解A~F
  • Windows网络「SSL错误问题」及解决方案
  • python数据可视化
  • LV.12 D18 中断处理 学习笔记
  • 蓝桥杯每日一题2023.11.19
  • <b><strong>,<i><em>标签的区别
  • c++中的特殊类设计
  • 开源更安全? yum源配置/rpm 什么是SSH?
  • 庖丁解牛:NIO核心概念与机制详解 04 _ 分散和聚集
  • Java读写Jar