Springboot Integrates with Swagger
Why need swagger
Swagger helps users to build, document, test and consume RESTful web services. It facilitates better API management within teams and projects.
Integration in official way
Only two steps:
(1) Add dependencies
<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>
(2) Add annotations
@EnableSwagger2
@SpringBootApplication
public class SpringbootMongoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMongoApplication.class, args);
}
}
After the two steps, just start up the Spring Boot
application and visit: http://localhost:8080/swagger-ui.html
Integration with SpringForAll
With the library, it’s also quite easy to use Swagger2 in Spring Boot.
- add dependency in
pom.xml
:
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
- add annotation
@EnableSwagger2Doc
:
@EnableSwagger2Doc
@SpringBootApplication
public class Bootstrap {
public static void main(String[] args) {
SpringApplication.run(Bootstrap.class, args);
}
}
By default, it will help to generate all the APIs doc in SwaggerUI.