工具具体功能就不多解释了,直接步入主题
导入相关依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
然后在config文件夹里创建SwaggerConfig类(名称不固定只是为了方便识别功能)
package com.example.demo.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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableOpenApi
// @EnableKnife4j
// @EnableSwagger2
public class Knife4jConfig {
@Bean
public Docket docket() {
Docket docket = new Docket(DocumentationType.OAS_30)
.apiInfo(new ApiInfoBuilder()
.title("我的标题")//下面几处文字都可以根据自己需要更改
.description("我的描述")
// .termsOfServiceUrl("http://www.xx.com/")
.contact(new Contact("corder", "www.juejin.cn", "xx@qq.com"))
.version("1.0")
.build())
// 分组名称
.groupName("all")
.select()
// 这里指定Controller扫描包路径(改为自己的Controller类的文件夹)
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
一定不要忘记在application.yml里进行这项mvc配置
mvc:
pathmatch:
matching-strategy: ANT_PATH_MATCHER
额外配置:
还可以在Controller里添加@Api(tags = "")和 @ApiOperation("")注解让接口文档更易读,如下
@RestController
@RequestMapping("/user")
@Api(tags = "用户")
public class UserController {
@Resource
private UserService userService;
//用户注册
@ApiOperation("注册")
@PostMapping("/register")
public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest registerRequest){
}
这时Controller类和方法就有了易读的名字