Table of Contents
Rest Documentation Swagger 2
Spring Boot 2 RESTful API Documentation With Swagger 2
What have we learned so far,
In this post, we discussed the steps to validate the input bean in Spring Rest.
Swagger 2
Swagger UI allows to visualize and interact with the API’s resources without having any of the implementation logic in place. It’s automatically generated from your OpenAPI (formerly known as Swagger) Specification, with the visual documentation making it easy for back end implementation and client side consumption.
The Swagger 2 specification, which is known as OpenAPI specification, has several implementations. Currently, Springfox that has replaced Swagger-SpringMVC (Swagger 1.2 and older) is popular for Spring Boot applications. Springfox supports both Swagger 1.2 and 2.0.
We will be using Springfox in our project. To use Swagger in our project, we need to do following things :
Step 1 – Add Dependency
Add the following dependency declaration in our Maven POM.
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> <scope>compile</scope> </dependency> <!-- UI will be used to read the swagger file generated by Swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> <scope>compile</scope> </dependency>
Step 2 – Create Docker Object
@Configuration @EnableSwagger2 public class SpringRestAdvanceConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } }
Step 3 – Run the Application
/v2/api-docs – this link will expose the documentation in json format for all rest endpoints, We can share this json file to clients to share the api information.
/swagger-ui.html – It will give UI for json file of above documentation.
http://localhost:8088/v2/api-docs
http://localhost:8088/swagger-ui.html
You can find the postman collection of this request at – CRUD Rest Example Postman Collection
Source Code
Download source code of Spring Rest Advance Topics from below git repository :
spring-rest-advance-topics
Spring Rest Advanced Tutorial
Lets go to our next tutorial where we will discuss below point
3. Spring Boot with H2 DB
– In this post we will go through below topics,
– What is in memory database ?
– What is the need of H2 database ?
– How to configure H2 DB with Spring Boot ?
– How to use data.sql and schema.sql in H2 DB ?
Blog URL – Part 3 – Spring Rest Advance : Spring Boot with H2 DB