Springboot 3整合Knife4j(SwaggerOpenApi3)
在Spring Boot 3整合Knife4j(Swagger 3、OpenAPI 3),你需要做以下几步:
- 在
pom.xml
中添加Knife4j的依赖:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
- 配置Knife4j属性,通常在
application.yml
或application.properties
中:
knife4j:
enable: true
# 其他个性化配置
- 在Spring Boot启动类上添加
@EnableKnife4j
注解:
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableKnife4j
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
- 创建一个API文档的配置类,如果需要自定义文档的信息:
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
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.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@EnableKnife4j
public class Knife4jConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket=new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.yourproject.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Knife4j 示例")
.description("这是一个示例")
.contact(new Contact("开发者名字", "http://url.to.your.site", "email@example.com"))
.version("1.0")
.build();
}
}
- 确保你的Controller类使用了Swagger注解,如
@Api
、@ApiOperation
等:
import io.swa
评论已关闭