Configuring Swagger to test rest api

Configure Swagger with Spring Boot rest application


In our last post, we have created a restful web service using Spring Boot.In this we will quickly configure swagger to our rest services and test the api.
It is simpler to configure swagger than you imagined. We need to add below dependencies in our pom.xml.

                <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>

Then add a new class to com.employee.config folder.

package com.employee.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.basePackage("com.employee.controller"))              
          .paths(PathSelectors.any())                          
          .build();                                           
    }
}

You can use various swagger annotations to provide a description to your service. For example, I am using @ApiOperation to describe my rest URLs (result highlighted in below image).


Hit the below URL in the browser to see the output.

http://localhost:8085/employee-service/swagger-ui.html




configure swagger with spring boot spring boot



Spring Restful Web Services - Download CodeCode from Github 

Restful web services using Spring Boot


Give you suggestion in comments.If you like the post Share with your friends on social network.



Configuring Swagger to test rest api Configuring Swagger to test  rest api Reviewed by JavaInstance on 4:55:00 AM Rating: 5

No comments:

Powered by Blogger.