【问题标题】:Springfox Type javax.servlet.http.HttpServletRequest not presentSpringfox 类型 javax.servlet.http.HttpServletRequest 不存在
【发布时间】:2022-12-23 06:01:16
【问题描述】:

我正在尝试使用 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'
...
}

春季启动类

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

招摇配置

@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 BOot 3 适用于 JakartaEE不是JavaEE。使用 2.6 而不是 3.0,afaik 没有可用于 Spring Boot 3 的 SpringFox 版本(主要是因为该版本仍在开发中,并且在大约 6 到 7 个月内不会成为最终版本)。
  • @M.Deinum,非常感谢。在寻找解决方案时,我在 GitHub 上发现了指示的问题:github.com/springfox/springfox/issues/3983 在您回答之前,我找不到 Spring 版本支持的指示。

标签: spring spring-boot springfox


【解决方案1】:

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 的最新发布版本)。

【讨论】:

    【解决方案2】:

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

    来源:Failed to start bean 'documentationPluginsBootstrapper' in spring data rest

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-16
      • 2020-03-16
      • 2023-02-16
      • 1970-01-01
      • 2016-12-23
      • 2020-08-19
      • 2019-01-12
      • 1970-01-01
      相关资源
      最近更新 更多