Backend Development 4 min read

How to Fix Spring Boot 3 and Swagger 2 Compatibility Issues

This guide explains why Spring Boot 3 is incompatible with Swagger 2, shows how to downgrade or replace dependencies, updates servlet imports, adjusts Maven and application settings, and provides a complete Swagger configuration to get the API docs running again.

Java Captain
Java Captain
Java Captain
How to Fix Spring Boot 3 and Swagger 2 Compatibility Issues

Spring Boot 3 and Swagger 2 are not compatible because Swagger 2 relies on the javax packages while Spring Boot 3 uses the jakarta packages. The usual workaround is to downgrade to Spring Boot 2.x or switch to a Jakarta‑compatible documentation tool such as Springdoc.

To keep using Swagger 2 with Spring Boot 3, first check the Spring framework version in pom.xml . If you are on the latest 3.3.1, change the Spring Boot version to 2.7.2 and refresh the project.

After the downgrade, classes that import HttpServletRequest and HttpServletResponse will still error because they reference javax.servlet . Replace those imports with the Jakarta equivalents and add the following Maven dependency:

<code><dependency>
  <groupId>jakarta.servlet</groupId>
  <artifactId>jakarta.servlet-api</artifactId>
  <version>6.0.0</version>
  <scope>provided</scope>
</dependency></code>

Import statements should be changed to:

<code>import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;</code>

If you encounter runtime failures, verify that your Java version is supported (Java 21 may cause issues). Also add the following property to application.properties to avoid a conflict between Spring Boot 2.6+ and Swagger:

<code>spring.mvc.pathmatch.matching-strategy=ant_path_matcher</code>

Next, add the Swagger 2 dependencies to pom.xml :

<code><dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency></code>

Create a configuration class in the config package:

<code>@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * API documentation meta‑information
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Demo Project API")
                .description("Learning Swagger2 with a demo project")
                .version("1.0")
                .build();
    }
}
</code>

After rebuilding, the application starts successfully and the Swagger UI can be accessed at http://localhost:8080/swagger-ui.html .

JavaConfigurationSpring BootAPI DocumentationCompatibilityJakartaSwagger 2
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.