springboot中使用knife4j接口文档

张开发
2026/5/26 15:05:34 15 分钟阅读
springboot中使用knife4j接口文档
1.引入依赖!-- Source: https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-openapi3-jakarta-spring-boot-starter -- dependency groupIdcom.github.xiaoymin/groupId artifactIdknife4j-openapi3-jakarta-spring-boot-starter/artifactId version4.5.0/version scopecompile/scope /dependency2.创建config文件夹创建Swaggerconfig文件Configuration public class SwaggerConfig { Bean public OpenAPI customOpenAPI() { return new OpenAPI() .info(new Info() .title(SpringBootvue3宠物网站) .version(1.0) .description(宠物网站接口文档) .contact(new Contact().name(LQ).url(www.lq.com))); } }3.使用OpenAPI3的规范注解注释各个Spring的REST接口示例代码如下RestController RequestMapping(body) Tag(name body参数) public class BodyController { Operation(summary 普通body请求) PostMapping(/body) public ResponseEntityFileResp body(RequestBody FileResp fileResp){ return ResponseEntity.ok(fileResp); } Operation(summary 普通body请求ParamHeaderPath) Parameters({ Parameter(name id,description 文件id,in ParameterIn.PATH), Parameter(name token,description 请求token,required true,in ParameterIn.HEADER), Parameter(name name,description 文件名称,required true,inParameterIn.QUERY) }) PostMapping(/bodyParamHeaderPath/{id}) public ResponseEntityFileResp bodyParamHeaderPath(PathVariable(id) String id,RequestHeader(token) String token, RequestParam(name)String name,RequestBody FileResp fileResp){ fileResp.setName(fileResp.getName(),receiveName:name,token:token,pathID:id); return ResponseEntity.ok(fileResp); } }4.使用 http://服务器地址:端口/context-path/doc.html 访问接口文档

更多文章