Adding Swagger on Spring Rest.
ℹ️ This article is a simple step-by-step.
Start adding the dependency on the pom.xml
.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
After that you should create the configuration. On my example below I'm using everything as default, and scanning the package that I need.
@Configuration
@Import({
org.springdoc.webmvc.ui.SwaggerConfig.class,
org.springdoc.core.configuration.SpringDocConfiguration.class,
org.springdoc.webmvc.core.configuration.SpringDocWebMvcConfiguration.class,
org.springdoc.core.properties.SpringDocConfigProperties.class,
org.springdoc.core.properties.SwaggerUiConfigParameters.class,
org.springdoc.core.properties.SwaggerUiConfigProperties.class,
org.springdoc.core.properties.SwaggerUiOAuthProperties.class
})
@ComponentScan(basePackages = {"org.springdoc"})
public class OpenApiConfig {
@Value("${build.version}")
private String buildVersion;
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info()
.title("Lorem Ipsum API")
.version(buildVersion)
.description("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."));
}
}
It's important to remember to add the version on the application properties. As the example (application.yml
):
build.version: @project.version@
This way the version will be read from the pom.xml
, meaning that you only need to change this at a single place.
Now you can connect: localhost:${PORT}/${CONTEXT-PATH}/swagger-ui/index.html
.