M N*_*man 2 java spring-boot springfox swagger-3.0
我正在尝试在我的 Spring Boot 项目中实现 swagger。我在网上搜索了一下,发现springfox 3.0可能与spring boot 3.0不兼容。我也尝试过旧版本。但我无法做到这一点。我正在使用gradle。
终端错误
构建.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.1'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'practice.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '19'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation("io.springfox:springfox-swagger2:3.0.0")
}
tasks.named('test') {
useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)
主java文件
package practice.example.crud_practice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@RestController
public class CrudPracticeApplication {
public static void main(String[] args) {
SpringApplication.run(CrudPracticeApplication.class, args);
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any()) // this will include all controllers
.paths(PathSelectors.any())
.build();
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
Run Code Online (Sandbox Code Playgroud)
当我评论以下行时,终端显示没有错误
@EnableSwagger2
Run Code Online (Sandbox Code Playgroud)
Springfox 3 似乎与 Spring Boot 3 不兼容。
原因:
HttpServletRequest正如您可以从包中看到Springfox 3 的引用javax.servlet.http。jakarta.servlet.httpSpring Boot 3通过其依赖项捆绑新包tomcat-embed-core。解决方案:使用Spring Boot 2.x
如果您想使用 Springfox 3,您可能应该坚持使用 Spring Boot 2.x 以获得一个可用的系统。
替代方案:迁移到springdoc-openapi
您也可以使用springdoc-openapi v2,它支持 Spring Boot 3。我已按照您的场景的迁移指南进行操作,并且运行良好(URL 为localhost:8080/v3/api-docs)。
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'依赖项Docketbean 和 springfox 导入springdoc.pathsToMatch=/**到application.properties| 归档时间: |
|
| 查看次数: |
5698 次 |
| 最近记录: |