Springfox 类型 javax.servlet.http.HttpServletRequest 不存在

Sha*_*571 21 spring spring-boot springfox

我正在尝试使用SpringFox。

Spring Boot 版本:'org.springframework.boot:3.0.0-SNAPSHOT'

构建.gradle

dependencies {
...
  implementation 'io.springfox:springfox-petstore:2.10.5'
  implementation "io.springfox:springfox-swagger2:3.0.0"
  implementation "io.springfox:springfox-oas:3.0.0"
  implementation 'io.springfox:springfox-swagger-ui:3.0.0'
...
}
Run Code Online (Sandbox Code Playgroud)

Spring引导类

@SpringBootApplication
@EnableSwagger2
@EnableOpenApi
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

SwaggerUiWebMvc配置器

@Component
public class SwaggerUiWebMvcConfigurer implements WebMvcConfigurer {
    private final String baseUrl;

    public SwaggerUiWebMvcConfigurer(
        @Value("${springfox.documentation.swagger-ui.base-url:}") String baseUrl) {
        this.baseUrl = baseUrl;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
        registry.
            addResourceHandler(baseUrl + "/swagger-ui/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
            .resourceChain(false);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(baseUrl + "/swagger-ui/")
            .setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
    }
}
Run Code Online (Sandbox Code Playgroud)

Swagger配置

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket petApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("full-petstore-api")
            .apiInfo(apiInfo())
            .select()
            .paths(any())
            .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("API")
            .description("Service API")
            .termsOfServiceUrl("http://springfox.io")
            .contact(new Contact("springfox", "", ""))
            .license("Apache License Version 2.0")
            .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
            .version("2.0")
            .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().anonymous().and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    }
}
Run Code Online (Sandbox Code Playgroud)

当我启动任务“bootRun”时,出现错误:

[  restartedMain] o.s.boot.SpringApplication: Application run failed

java.lang.TypeNotPresentException: Type javax.servlet.http.HttpServletRequest not present
    at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117) ~[na:na]
    at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125) ~[na:na]
...

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
Run Code Online (Sandbox Code Playgroud)

我尝试使用 SpringFox 演示项目示例和 SpringFox-Boot,但每次都会出现此错误。

Meh*_*imi 31

简短实用的解决方案:

对于spring-bootswagger-ui之间的集成,删除所有Springfox相关依赖项,只需将以下库添加到您的项目依赖项列表中(无需额外配置):

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.3.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

然后, Swagger UI页面将位于

http://localhost:8080/swagger-ui/index.html


更多细节:

Swagger (当前称为OpenAPI )是一种用于可视化 Web 服务的规范。Springfox是该规范的实现,其所有者不再维护,该项目的最后一次提交是在2020 年 10 月。当时,Spring 框架使用了命名空间,但在较新版本的 Spring 中,命名空间已更改为.javaxjakarta

Springfox仍然使用旧版本的命名空间(javax),当然它与新版本的Spring框架不匹配,所以你必须:

  • 将您的Spring-Boot版本降级到旧版本 (2.7) 或
  • 使用另一个OpenAPI实现。

我建议您改用springdoc-openapi库。根据官方文档

springdoc-openapi java 库有助于使用 spring boot 项目自动生成 API 文档。springdoc-openapi 的工作原理是在运行时检查应用程序,根据 spring 配置、类结构和各种注释推断 API 语义。


注意:

  • 如果您有 context-path 那么http://server:port/context-path/swagger-ui.html
  • 对于 spring-boot v3.x 支持,请确保使用 springdoc-openapi最新版本


M. *_*num 29

Spring Boot 3.0 是为 Java17 和 JakartaEE 而构建的,而不是JavaEE。Spring Boot 3 仍在开发中,尚未发布最终版本。目前还没有正式的发布日期,但预计不会早于 2022 年第四季度。在那之前,我不会考虑将 Spring Boot 3 用于生产(或者也许当他们开始发布候选版本时)。

话虽如此,目前还没有支持 JakartaEE 的 SpringFox 版本,因此无法与 Spring Boot 3 一起使用(请参阅)。因此,在这个问题得到解决之前,SpringFox 和 Spring Boot 3 的组合将无法工作。

使用 2.6.x(目前是 Spring Boot 的最新发行版本)。


小智 9

只需将此依赖项添加到 pom.xml 中即可

https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

如有必要,还可以将此属性添加到您的 application.properties 中

spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

来源:无法在 Spring Data Rest 中启动 bean 'documentationPluginsBootstrapper'