404 Not Found: Troubleshooting Swagger API Docs in Spring Boot
Problem: You're integrating Swagger into your Spring Boot application using Springfox (formerly swagger-springmvc) and are encountering a "404 Not Found" error when trying to view your API documentation.
Rephrasing: Imagine you've just built a fancy restaurant, but the menu is missing! Your customers can't see what you offer, and they're frustrated. This is similar to encountering a 404 error with Swagger – your API documentation is supposed to be available, but it's lost in cyberspace.
Scenario & Code:
Let's say you're using the following basic Springfox setup in your application:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Despite this, you're getting a 404 error when navigating to http://localhost:8080/swagger-ui.html
or http://localhost:8080/v2/api-docs
.
Why is this happening?
The "404 Not Found" error usually indicates one of the following:
- Incorrect Configuration: The most common culprit is a mismatch between your Springfox configuration and the actual location of your API documentation.
- Missing Dependencies: The Springfox libraries are not correctly added to your project.
- Security Issues: Your application's security configuration might be blocking access to the Swagger UI endpoint.
- Mismatched URL: You're attempting to access the documentation using the wrong URL.
Troubleshooting Tips:
-
Double-check your dependencies: Ensure that you have the correct Springfox dependencies in your
pom.xml
orbuild.gradle
file. Here's a typical setup:<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency>
-
Verify your Springfox configuration: Review your
SwaggerConfig
class and make sure it accurately defines theDocket
object. This ensures that Swagger correctly scans and documents your APIs. For instance, if you want to only document specific controllers or packages, you should modify theapis()
andpaths()
methods accordingly. -
Check your security configuration: If your application uses Spring Security, ensure that the Swagger UI endpoint is accessible. You might need to explicitly allow access using the
@EnableWebSecurity
annotation or by configuring a customWebSecurityConfigurerAdapter
. -
Verify your URL: Make sure you're accessing the correct URL. The default URL for the Swagger UI is
http://localhost:8080/swagger-ui.html
, while the API documentation endpoint is usuallyhttp://localhost:8080/v2/api-docs
. These may vary based on your application's configuration. -
Debugging: Use your IDE's debugger to inspect the flow of your Springfox configuration. Look for any errors or exceptions that might be preventing the Swagger UI from being initialized properly.
Additional Tips:
- Restart your application: Sometimes a simple restart can resolve the issue.
- Clear your browser cache: If you've made changes to your application or configuration, clearing your browser cache might be necessary.
- Check the Springfox documentation: The official Springfox documentation https://springfox.github.io/springfox/ provides in-depth guidance and examples.
By following these troubleshooting steps and verifying your configuration, you can resolve the "404 Not Found" error and successfully view your API documentation using Swagger.