Configuring Swagger UI in your project

You can easily configure Swagger UI in your microservice project by using a built-in solution in the Swagger 2 library, which creates Swagger UI with the Swagger-generated API documentation. Let's configure another Maven dependency for the Swagger UI:

<dependency> 
   <groupId>io.springfox</groupId> 
   <artifactId>springfox-swagger-ui</artifactId> 
   <version>${swagger.version}</version> 
</dependency> 

We have added a dependency with groupId io.springfix and the springfox-swagger-ui artifact ID with the given version. This is enough to configure Swagger UI in your project to see API documentation. All required HTML pages will be rendered by this library only, we don't need to place any HTML for the Swagger UI.

Let's run your Spring Boot Account microservice project again and you can test it in your favorite browser by accessing following URL:

http://localhost:6060/swagger-ui.html#/

It renders Swagger UI as follows:

It renders Swagger UI for your Spring Boot project's Account microservice. It displays a list of controllers because Swagger scans your project code and exposes the documentation for all controllers. The client can use this URL and Swagger UI to learn how to use your REST APIs. It displays all HTTP methods to call for each URL and also displays input documents to send and the status code to expect.

In the preceding screenshot of Swagger UI, let's click on any controller from the rendered list. It will render a list of HTTP methods, such as DELETE, GET, HEAD, OPTIONS, PATCH, POST, and PUT. Click on any method in Swagger UI and it will render additional details, such as content type, response status, and parameters. You can test this method using a Try it Out button on Swagger UI.

As you can see the following screenshot, when I clicked on the account controller link in Swagger UI, it expands with available methods of the account controller (as you can see, delete() with HTTP DELETE, all() with HTTP GET, save() with HTTP POST, and update() with HTTP PUT):

In the preceding screenshot, click on any of the delete(), all(), save(), or update() methods and Swagger UI will display the following UI:

It has displayed the details such as Response Status, Input Parameters, Response Content Type, and HTTP Status Code. You can also use the Try it out button to test this API method.

So far, we have created API documentation for only one controller, AccountController, Swagger can easily synchronize with your code base if you add another controller, CustomerController, to the same application. Let's add the following controller:

@RestController 
public class CustomerController { 
    
   @GetMapping("/customer/name") 
   public String customerName(){ 
         return "Arnav Rajput"; 
   } 
    
   @PostMapping("/customer/name") 
   public String addCustomerName(){ 
         return "Aashi Rajput"; 
   } 
} 

After adding the controller and refreshing your browser with Swagger UI, it will display a list of controllers, including CustomerController:

You have seen the default meta-configuration provided by the Swagger, you can also customize this meta-configuration according to your application parameters by using the Docket bean. Let's see how to add extra configuration in your Spring Boot project with Swagger.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset