Home > Net >  Type javax.servlet.http.HttpServletRequest not present, with gradle springfox-swagger2: 3.0
Type javax.servlet.http.HttpServletRequest not present, with gradle springfox-swagger2: 3.0

Time:01-11

I'm trying to Implement swagger in my spring boot project. I searched on internet and found that springfox 3.0 might not compatible with spring boot 3.0. I tried with the older versions as well. But I'm not able to do that. I'm using gradle.

ERROR IN TERMINAL

enter image description here

Build.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()
}

main java file

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

WHEN I COMEMNT THE FOLLOWING LINE OUT TERMINAL SHOWS NO ERROR

@EnableSwagger2

CodePudding user response:

It seems Springfox 3 is not compatible with Spring Boot 3.

Reason:

  • As you can see Springfox 3 references HttpServletRequest from the package javax.servlet.http.
  • Spring Boot 3 bundles the new jakarta.servlet.http package via its tomcat-embed-core dependency.

Solution: Use Spring Boot 2.x

If you want to use Springfox 3, you should probably stick to Spring Boot 2.x to have a working system.

Alternative: Migrate to springdoc-openapi

You may also use springdoc-openapi v2 instead, which supports Spring Boot 3. I have followed the migration guide for your scenario and it worked fine (URL is localhost:8080/v3/api-docs).

  • Removed springfox dependency
  • Added implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2' dependency
  • Removed the Docket bean and springfox imports
  • Added springdoc.pathsToMatch=/** to application.properties
  • Related