Spring boot with Swagger2 API documentation(Part4)

Prerequisites1. Git Installed in your pc
2. IntelliJ IDEA Community Edition
3. Java Installed in your PC (8 to 11) any version
5. Json Viewer Plugins for Chrome Browser (https://github.com/tulios/json-viewer)
6. Docker and Docker Compose
> git clone https://github.com/tariqulislam/spring-boot-docker.git

Project Structure of spring-boot-docker in IntelliJ Idea

To Run the Project In Local environment

If you have MySQL Server in the local environment or PC, And Want to run the project with Local Env, change the data source URL to localhost and change the dialect as a locally installed MySQL server in application.properties file.

Configure the local MySQL Server with Spring boot

To Run the project in Docker environment

> docker-compose up --build

Configure the Spring Boot Application with Springfox Swagger2

> git checkout -b feature/swagger_integration
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
package com.example.starter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class StarterApplication {

public static void main(String[] args) {
SpringApplication.run(StarterApplication.class, args);
}

}
# First Stop the services by following command
> docker-compose down

# Then rebuild the project by following command
> docker-compose up --build

To run in Program in IntelliJ Idea you have to follow the (Figure 1)

http://localhost:4000/v2/api-docs

Install the Springfox Swagger2 UI to show the API endpoint with Presentational View

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

Down the docker service and run it again and go to browse with the following links. Or Local Env, you need to restart the spring boot application from Intellij Idea Tool bar

http://localhost:4000/swagger-ui.html

About Docket

There is a special Object named Docket by this, we can override the configurations related to Swagger in the spring boot project.

About ApiInfo for Swagger Documentation

Line 3: Take the title of the Swagger UI Endpoint
Line 4: Take the Description of the Endpoint
Line 5: Version number of documentaion
Line 6: Terms and Service Information for documentaiton
Line 7: Contact Address for admin using springfox Contact Object
Line 10: Licence Name
Line 11: Licence Url
@Bean
public Docket swaggerConfiguration() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(PathSelectors.ant("/api/v1/**"))
.apis(RequestHandlerSelectors.basePackage("com.example.starter"))
.build()
.apiInfo(apiDetails());
}
> docker-compose up --build
http://localhost:4000/swagger-ui.html

Add the Edition Documentation to API Endpoint

@ApiOperation(value = "Show All Members",
notes = "This will provide the the member information",
response = Member.class)
public List<Member> all() {
return memberService.getAllMembers();
}
@PutMapping("/members/{id}")
public ResponseEntity<Member> updateMember(
@Valid @RequestBody Member member,
@ApiParam("Pass the Member id Find the Member Existing Info")
@PathVariable(value= "id") Long id) {
return ResponseEntity.ok(memberService.updateMember(member, id));
}
@ApiModel(description = "This is Member Model")
public class Member {}
@ApiModelProperty(dataType = "Integer" , example = "123")
private Long id;

API Interaction with Swagger UI

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store