开启swagger文档,直接上教程。
<!--swagger 依赖-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
package com.test.empback.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
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;
/**
* @author: 公众号:知识浅谈 个人微信:CYF2918CYF
* Package: com.test.empback.config
* @date: 2023/9/3 15:40
* @Description: swagger配置
* @version: 1.0
*/
@Configuration
@EnableKnife4j
public class Knife4jConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.test.empback<这个地方改成你的包名>"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Springboot项目 后台服务API接口文档")
.description("使用 knife4j 搭建的后台服务API接口文档")
.contact(new Contact("你的姓名","你的网站","你的邮箱"))
.version("1.0.0")
.build();
}
}
@Api("公告相关控制器")
@RequestMapping("/")
public class TestController {
@GetMapping("/test")
public Result get(){
return "test";
}
}
然后运行项目: 打开网址 http://localhost:你设置的端口,没有设置默认为8080/api/doc.html
我的设置的8081,所以我就访问http://localhost:8081/api/doc.html