将knif4j快速集成到springboot项目中

快速集成

环境及坐标

需要提前搭好springboot 项目的环境,搭建成功后添加如下依赖(springboot 已经对依赖进行了版本控制, 所以这里没有声明版本号)

	<dependency>
	     <groupId>com.github.xiaoymin</groupId>
	     <artifactId>knife4j-spring-boot-starter</artifactId>
	</dependency>

下面是配置类

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
	@Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)      // 选择swagger2版本
                .apiInfo(apiInfo())         //定义api文档汇总信息
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.xxx.xxxxxx"))  // 指定生成api文档的包
                .paths(PathSelectors.any())     // 指定所有路径
                .build();
    }

    /**
     * 构建文档api信息
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("文档标题")     // 文档标题
                .description("描述")      //描述
                .version("1.0.0")     //文档版本号
                .termsOfServiceUrl("http://localhost:8080")     //网站地址
                .build();
    }
}

启动项目, 访问"http://host:port/doc.html"正常进入就是成功

下面是注解, 及一些用法

1.@Api注解(类注解)
/**
 * @author qinple
 * 用法示例如下: 一般写在接口上
 */
@Api(value = "头条频道管理", tags = "channel", description = "频道管理API(帮助文档)")
public interface ChannelControllerApi{ 
	//.....你的代码
}
2.@ApiOperation注解(方法注解)
	/**
	 * 用法示例如下:一般写在接口的抽象方法上
	 */
	@ApiOperation("查询频道列表,根据名称进行分页模糊查询, 返回分页查询结果")
    public abstract ResponseResult findByNameAndPage(ChannelDto channelDto);
3.@ApiModelProperty注解(属性注解)
	/**
     * 用法示例如下: 一般用在实体类上,如dto等
     * required 属性默认为false
     */
	@ApiModelProperty(value = "频道ID")
    private Integer id;

    @ApiModelProperty(value = "频道名称", required = true)
    private String name;
    
    @ApiModelProperty(value = "频道描述", required = false)
    private String description;

大致的就这些, 如有问题留言提醒, 谢谢.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Sure! To integrate Spring Boot with Knife4j, you can follow these steps: 1. Add the required dependencies to your project's `pom.xml` file: ```xml <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> ``` 2. Create a new configuration class, typically named `SwaggerConfiguration`, and annotate it with `@Configuration`: ```java @Configuration public class SwaggerConfiguration { } ``` 3. Inside the `SwaggerConfiguration` class, add the necessary bean definitions for `Docket` and `ApiInfo`: ```java @Configuration public class SwaggerConfiguration { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) // Set your controller package .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Your API Documentation") .description("Description of your API") .version("1.0.0") .build(); } } ``` 4. Make sure to update the `basePackage` value in the `RequestHandlerSelectors` to match the package where your Spring MVC controllers are located. 5. Run your Spring Boot application, and you should be able to access the Knife4j swagger UI at `http://localhost:8080/doc.html`, where `8080` is your application's port. That's it! You have now integrated Knife4j with your Spring Boot application. You can customize the Swagger configuration further according to your requirements.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qin_pgle_CSDN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值