Springfox vs Springdoc OpenAPI: Which Swagger Implementation is Best for Spring Boot ?

Chandan Kortekar
4 min readNov 16, 2024

--

The de facto standard for REST API documentation is Swagger, that offers interactive documentation to improve API usage. Swagger tools are frequently used by developers, and Springfox and Springdoc OpenAPI are the two most frequently utilised implementations for Spring Boot apps. But which one ought you to pick?

✅ Understanding Swagger in Spring Boot

Swagger, now part of the OpenAPI Specification, provides:

  • Interactive Documentation: Helps developers and testers visualize APIs.
  • Try-It-Out Feature: Allows users to test API endpoints directly from the documentation.
  • API Contract Generation: Ensures accurate API specifications for consumers.

✅ Overview of Springfox

For many years, Springfox was the preferred library for integrating Swagger into Spring Boot applications. It creates OpenAPI 3.0 (/v3/api-docs) and Swagger 2.0 (/v2/api-docs) specifications and offers an interactive documentation Swagger user interface.

✅ Drawbacks of Springfox

Compatibility Issues: Springfox struggles with compatibility for newer versions of Spring Boot (2.6+). It often requires additional workarounds or patches to function.

Performance and Maintenance: Springfox is now stagnant and has unfixed bugs.

Complex Configuration: More Docket configuration is frequently needed for advanced customisation, which adds to the complexity.

✅ Overview of Springdoc OpenAPI

The more contemporary substitute for Springfox is Springdoc OpenAPI. It was created especially for OpenAPI 3.0, requires little setup, and integrates seamlessly with the most recent Spring Boot versions.

✅ Springdoc Features

Out-of-the-Box Support:Compatible with Spring Boot 3.x and fully supports OpenAPI 3.0.

Minimal Configuration:API documentation is generated automatically without the need for significant modifications.

Active Maintenance:Consistent updates guarantee compatibility with the most recent OpenAPI and Spring ecosystem standards.

Kotlin and Functional Endpoints: Native support for Kotlin and Spring WebFlux/Functional APIs.

✅ Why Springdoc OpenAPI is the Better Option

  1. Future-Proof : It is compatible with current versions of Spring Boot, guaranteeing compatibility for the long run.
  2. Simpler Integration : Springdoc saves development time by operating outside the box.
  3. Active Community : Consistent support and updates maintain its dependability and relevance.
  4. Enhanced Features : It enables sophisticated use cases such as functional endpoints, Kotlin integration, and API grouping.

✅ Step-by-Step Guide: Implementing Springdoc OpenAPI in Spring Boot

1. Add Dependencies

Add the following dependency to your pom.xml or Gradle

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'

2. Basic Configuration

By default, Springdoc OpenAPI works without any additional configuration. Once added, you can access:

3. Customizing API Documentation

To customize the API documentation path or title, add the following to your application properties :

springdoc.api-docs.path=/api-docs
springdoc.swagger-ui.path=/swagger-ui
springdoc.swagger-ui.title= my-api-docs

4. Annotating Your Endpoints

Springdoc automatically scans for standard Spring annotations like @RestController and @RequestMapping.

5. Advanced Features

Grouping APIs: Create multiple groups for better organization.

Security Configuration: Add authentication and authorization details to your API documentation.

Custom Models: Use annotations like @Schema to define custom API models.

❌ Common Errors in Springdoc Integration ❌

No static resource swagger-ui/index.html

🔴 Cause: Missing or incorrect dependency for Swagger UI.

🟢 Solution: Ensure the following dependency is added:

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>

✅ Swagger UI Not Loading or 404 Error

🔴Cause: Incorrect URL or Swagger UI path.

🟢 Solution: The default URL for Swagger UI in Springdoc is /swagger-ui, not /swagger-ui.html. Use /swagger-ui in the browser.

✅ API Documentation Not Showing Updates

🔴 Cause: Caching issues or outdated context.

🟢 Solution: Clear your browser cache and rebuild your project to refresh the API documentation.

✅ Missing API Documentation or No Endpoints Displayed

🔴 Cause: Incorrect or missing annotations on controllers.

🟢 Solution: Ensure your controllers are properly annotated with @RestController and endpoints with @GetMapping, @PostMapping, etc.

✅Compatibility Issues with Spring Boot 3+

🔴 Cause: Springdoc may require specific versions to work with Spring Boot 3.

🟢 Solution: Use Springdoc version 1.7.x for full compatibility with Spring Boot 3 and Jakarta EE.

Conclusion

Although Springfox was a useful tool for developers in the past, its inability to handle more recent versions of Spring Boot makes it less practical now. A reliable, user-friendly, and actively maintained substitute is provided by Springdoc OpenAPI. It is the recommended option for Swagger in Spring Boot apps due to its smooth integration and sophisticated functionality.

Use Springdoc OpenAPI to update your API documentation and take advantage of a cutting-edge, effective development environment!

👋 👍 🤝 😃 ………

--

--

Chandan Kortekar
Chandan Kortekar

Written by Chandan Kortekar

💬 Ask me about WEB, Devops( Docker, Jenkins , Kubernetes , gradle , GCP , Git / Gitlab , Java , Springboot , PostgreSQL, Terraforms, Junit , Linux, Neo4j,)

No responses yet