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

问题描述 投票:0回答:4

我正在尝试使用 SpringFox。

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

build.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'
...
}

Spring Boot 类

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

SwaggerUiWebMvcConfigurer

@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");
    }
}

SwaggerConfig

@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();
    }
}

安全配置

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

当我启动任务“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

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

spring spring-boot springfox
4个回答
27
投票

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

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

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


14
投票

您的 Spring-Boot 版本是 3,它使用

jakarta
命名空间而不是
javax

但是Swagger仍然使用旧版本命名空间(

javax
)。

它们不匹配,因此您必须将 Spring-Boot 版本降级到旧版本(2.7)或等待新版本的 Swagger。


我建议你使用 springdoc-openapi 代替。 Spring-Boot 3 的 OpenApi 兼容版本是这些依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.0.2</version>
    </dependency>

Swagger UI 页面将在以下位置提供:

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

P.S.如果您有上下文路径,则(http://server:port/context-path/swagger-ui.html


对于 spring-boot v3 支持,请确保使用 springdoc-openapi v2

https://springdoc.org/v2/


6
投票

只需将此依赖项添加到 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>

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

spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

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


0
投票

swagger不支持Spring Boot 3.0。使用openapi。这是更新后的招摇。

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>
© www.soinside.com 2019 - 2024. All rights reserved.