Springboot:knife4j集成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

目录

一、添加maven依赖

二、添加配置类

三、打开注解

四、对需要进行文档说明的类、接口、属性添加注解

五、成果展示

总结


提示:以下是本篇文章正文内容,下面案例可供参考

一、添加maven依赖

<!-- Knife4j依赖 -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.7</version> <!-- 请检查最新版本 -->
</dependency>
<!-- Swagger依赖 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version> <!-- 请检查最新版本 -->
</dependency>

二、添加配置类

 DocumentationType是可配的,可以选择高版本或者低版本

import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.finance.late.controller")) // 替换成你的Controller包路径
                .paths(PathSelectors.any())
                .build();
    }

}

三、打开注解

在启动类(带该注解的类 @SpringBootApplication)上添加注解:@EnableSwagger2

四、对需要进行文档说明的类、接口、属性添加注解

在类上添加注解

@Api(tags = "XXX模块")
public class LateController {
    ......
}

在接口上添加注解

@ApiOperation("XXXX方法")
@PostMapping ("/doSomething")
public Object doSomething(@RequestBody Object object) {
	......
}

 在属性上添加注解

@Data
@ApiModel("XXXObject")
public class XXXObject implements Serializable {

    private static final long serialVersionUID = 42L;

    @ApiModelProperty(value = "xxx描述", required = false)
    private String xxx;
    
    @ApiModelProperty(value = "yyy描述", required = true)
    private Object yyy;

}

该处required标明该字段是否必填

五、成果展示

访问链接:http://IP:port/doc.html

如果对应的项目有类似的配置:server.servlet.context-path = /project_name

访问链接就对应为:http://IP:port/project_name/doc.html

 


总结

没什么难度,简单到有手就行!

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值