Spring boot enable openapi swagger accessed
- Add the necessary dependencies to your Spring Boot project's
pom.xml
file:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.5</version> <!-- Use the latest version available -->
</dependency>
- Create a configuration class to customize the OpenAPI documentation. Annotate the class with
@Configuration
and extendWebMvcConfigurerAdapter
:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/")
.addResourceLocations("classpath:/META-INF/resources/webjars/springdoc-openapi-ui/")
.resourceChain(false);
}
}
- Optionally, customize the OpenAPI documentation by adding
application.properties
orapplication.yml
configuration:
springdoc:
api-docs:
path: /api-docs # Customize the path to access the OpenAPI JSON documentation
swagger-ui:
path: /swagger-ui.html # Customize the path to access the Swagger UI
Run your Spring Boot application.
Access the Swagger UI by navigating to the specified path (default is
http://localhost:8080/swagger-ui.html
) in your web browser.Explore the API documentation using the Swagger UI interface.
Note: Make sure your controllers and APIs are properly annotated with Swagger annotations such as @ApiOperation
, @ApiResponse
, and others to provide detailed information in the generated Swagger documentation.