Swagger2 的使用——后台可视化增删查改
前言
之前写了一篇文章,SpringBoot连接MySQL获取数据写后端接口
但是有一个问题,只能可视化测试查询语句,但是增删改不可以可视化操作,就不怎么方便,但是有Swagger2 可视化操作就会很简单,所以在此进行记录分享。
这样感觉会不会so easy呢,那就直奔主题。
引入依赖
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
增加工具类
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;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
.select()
.apis(RequestHandlerSelectors.basePackage("com.mybatis.mybatis.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot Swagger2 构建RESTful API")
.termsOfServiceUrl("http://www.github.io/")
.version("1.0")
.description("API 描述")
.build();
}
}
修改Controller注释
访问路径
http://localhost:8080/swagger-ui.html#
最后,如果对你有帮助,记得一键三连哦
如果有错误,恳请批评指正。
焦虑往往是想的多,干的少,加油陌生人