springboot低版本整合knife4j

springboot低版本整合knife4j

参考网址:

knife4j的gitee仓库

https://search.gitee.com/?skin=rec&type=repository&q=knife4j

knife4j示例

https://gitee.com/xiaoym/swagger-bootstrap-ui-demo

该项目knife4j整合单体springboot,以及knife4j整合springcloud,可以说非常齐全了,开箱即用

参考knife4j示例的knife4j-lower-spring-boot-demo项目

重点说明:

这个是个配置模板,防止以后使用找不到

问题在线

公司最近一个项目是2017年的springboot项目,使用的springboot版本是1.x版本,整合最新的knife4j会出现各种问题

pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.11.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.xiaominfo</groupId>
	<artifactId>knife4j-lower-spring-boot-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>knife4j-lower-spring-boot-demo</name>
	<description>Spring Boot低版本的测试</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

knife4j配置类

package com.xiaominfo.knife4j.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class SwaggerConfiguration {


    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("低版本版本")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xiaominfo.knife4j.demo.web"))
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui-demo RESTful APIs")
                .description("# swagger-bootstrap-ui-demo RESTful APIs")
                .termsOfServiceUrl("http://www.xx.com/")
                .contact("xx@qq.com")
                .version("1.0")
                .build();
    }

}

说明:

apis(RequestHandlerSelectors.basePackage(“com.xiaominfo.knife4j.demo.web”))指定对应controller包路劲即可

重点总结

如果项目中在线接口文档出现问题,我们可以使用springboot+knife4j这种简洁的风格

优势

  1. 不需要整合swagger

  2. 配置简单,添加knife4j依赖+knife4j配置类即可

  3. knife4j比swagger更加简洁

  4. 这种方案可以兼容现阶段springboot高版本

**<u>*兼容很重要!!!*</u>**




个人csdn博客网址:https://blog.csdn.net/shaoming314

jam

个人博客网址:www.shaoming.club

halo

勇敢牛牛,不怕困难

img
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以按照以下步骤在Spring Boot中整合Knife4j(原Swagger): 1. 在您的Spring Boot项目中添加Knife4j的依赖。在您的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> ``` 2. 在您的Spring Boot配置文件(application.properties或application.yml)中配置Knife4j。 对于application.properties,添加以下配置: ```properties # 配置Knife4j的扫描包路径 springfox.documentation.swagger.v2.path=/swagger # 配置Knife4j的UI页面标题 knife4j.title=Your API Documentation # 配置Knife4j的UI页面描述 knife4j.description=API Documentation for Your Project # 配置Knife4j的UI页面联系人信息 knife4j.contact.name=Your Name knife4j.contact.url=Your Website knife4j.contact.email=Your Email ``` 对于application.yml,添加以下配置: ```yaml springfox: documentation: swagger: v2: path: /swagger knife4j: title: Your API Documentation description: API Documentation for Your Project contact: name: Your Name url: Your Website email: Your Email ``` 3. 在您的控制器类或方法上使用Swagger注解来生成API文档。例如: ```java @RestController @RequestMapping("/api") @Api(tags = "API") public class ApiController { @ApiOperation("获取用户信息") @GetMapping("/user/{id}") public User getUser(@PathVariable Long id) { // 实现逻辑 } } ``` 4. 运行您的Spring Boot应用程序,并访问"http://localhost:8080/swagger"(根据您的实际端口和上下文路径进行调整)即可查看生成的API文档。 注意:这里的示例是基于Swagger 2.x版本Knife4j,如果您使用的是Swagger 3.x版本,配置可能会有所不同。请根据您使用的Knife4j版本进行相应的配置。 希望这个简单的步骤能够帮助您整合Knife4j到您的Spring Boot项目中。如有其他问题,请随时向我提问!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值