knife4j 整合 springboot的过程详解
作者:19783793
这篇文章主要介绍了knife4j整合springboot的过程,本次整合springboot版本为2.3.12,本文通过图文实例相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
官方文档:https://doc.xiaominfo.com/knife4j
版本兼容说明:https://doc.xiaominfo.com/docs/quick-start/start-knife4j-version
升级说明:https://doc.xiaominfo.com/docs/upgrading/upgrading-to-v4
版本兼容惯关系:
springboot 1.5.x~2.0.0 对应 <Knife4j 2.0.0
springboot 2.0 ~ 2.2 对应 Knife4j 2.0.0 ~ 2.0.6
springboot 2.2.x~2.4.0 对应 Knife4j 2.0.6 ~ 2.0.9
springboot 2.4.0~2.7.x 对应 >=Knife4j 4.0.0
1.引入依赖:
<!-- knife4j-spring-boot-starter:(3.0 ~ 3.0.3 是过度版本,官方不建议使用) -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>注意:本次整合springboot版本为2.3.12
2.配置类
Configuration
@EnableKnife4j
@EnableSwagger2WebMvc // 如果是 knife4j 3.x版本,则只需要去除掉该注解即可
public class SwaggerConfig {
private String basePackage = "com.xxx.xxx.controller";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.groupName("api")
.enable(true)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage)) // 基于包扫描
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // 基于注解
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API 接口文档")
.description("Restful API 接口文档")
.version("1.0")
.contact(new Contact("联系人姓名","联系人url","联系人email"))
.termsOfServiceUrl("服务条款URL")
.license("xxx License Version 1.0")
.licenseUrl("http://www.xxx.xxx/licenses/LICENSE-1.0")
.build();
}
}3.配置文件
# 版本建议:3.0~3.0.3 底层依赖springfox框架版本升级至3.0.3,OpenAPI规范是v3,过度版本,建议开发者不要使用 knife4j.enable=true # 是否开启生产环境屏蔽 true:关闭swagger,false:开启swagger # true - 设置未true报错:You do not have permission to access this page - 即生产环境禁止访问 knife4j.production=false knife4j.setting.language=zh-CN
4.编写代码Controller
@Api(tags = "测试接口")
@Controller
@RequestMapping("/test")
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@ApiOperation("set value 操作")
@ResponseBody
@RequestMapping(value = "/set", method = RequestMethod.POST)
public String setVal(String key, String value) {
redisTemplate.opsForValue().set(key, value);
return "success set val";
}
@ApiOperation("get 操作")
@ResponseBody
@RequestMapping(value = "/get", method = RequestMethod.GET)
public String getValue(String key) {
String result = (String) redisTemplate.opsForValue().get(key);
System.err.println("======> 返回结果result:" + result);
return result;
}
}5.访问与测试:http://localhost:8080/doc.html

到此这篇关于knife4j 整合 springboot的文章就介绍到这了,更多相关knife4j 整合 springboot内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- Springboot3集成Knife4j的步骤以及使用(最完整版)
- SpringBoot Knife4j框架&Knife4j的显示内容的配置方式
- SpringBoot与knife4j的整合使用过程
- springboot读取bootstrap配置及knife4j版本兼容性问题及解决
- springboot3整合knife4j详细图文教程(swagger增强)
- springboot整合knife4j全过程
- SpringBoot中使用Knife4J的解决方案
- springboot集成swagger3与knife4j的详细代码
- Springboot中整合knife4j接口文档的过程详解
- knife4j+springboot3.4异常无法正确展示文档
