官方文档: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.01.引入依赖:<!-- 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.122.配置类Configuration @EnableKnife4j @EnableSwagger2WebMvc // 如果是 knife4j 3.x版本,则只需要去除掉该注解即可 public class SwaggerConfig {private String basePackage = "com.xxx.xxx.controller";@Beanpublic 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-CN4.编写代码Controller@Api(tags = "测试接口") @Controller @RequestMapping("/test") public class TestController {@Autowiredprivate 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