springspring-bootspringfox

Springfox Type javax.servlet.http.HttpServletRequest not present


I'm trying to use SpringFox.

Spring Boot version: '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 Class

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

SecurityConfig

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

When I start task 'bootRun', I get an error:

[  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

I tried to use SpringFox demo projects examples and SpringFox-Boot, but I get this error every time.


Solution

  • Spring Boot 3.0 is built for Java17 and JakartaEE, not JavaEE. Spring Boot 3 is still under development and hasn't seen a final release yet. There has been no official release date yet, but expect nothing sooner than Q4 of 2022. Until then I wouldn't consider Spring Boot 3 for production (or maybe when they start releasing Release Candidates).

    That being said, there is currently no SpringFox release that supports JakartaEE and therefore isn't usable with Spring Boot 3 (see this). So until that has been resolved the combination of SpringFox and Spring Boot 3 won't work.

    Use 2.6.x (which at this moment is the latest release version of Spring Boot).