knife4j 整合 springboot

官方文档: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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值