Configure Swagger In Spring Boot

Today Agenda: Today we will configure the swagger, in the next 2 minutes you will be able to configure the swagger in your application.

When I was Configure the swagger first time I searched on google and I get many solutions but they treat me like I have the knowledge about the swagger that’s why I write this blog today if you going to configure the swagger first time in your career then focus on it and if have knowledge about the swagger but you have some doubts then don’t worry after this article your all doubts will be clear.

What is swagger

Swagger allows you to describe the structure of your APIs so that machines can read them. The ability of APIs to describe their own structure is the root of all awesomeness in Swagger. Why is it so great? Well, by reading your API’s structure, we can automatically build beautiful and interactive API documentation. We can also automatically generate client libraries for your API in many languages and explore other possibilities like automated testing. Swagger does this by asking your API to return a YAML or JSON that contains a detailed description of your entire API.
https://swagger.io/docs/specification/2-0/what-is-swagger/
Click on this link for a full description

Let’s Start

Required Tools
Maven
STS
JDK 8
(Attention)Read Each Line carefully

Project Structure

Project 1

Output

SwaggerConfig Class Definition

package com.tuts.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.swagger.web.ApiKeyVehicle;
import springfox.documentation.swagger.web.SecurityConfiguration;
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.tuts.controller"))
				.paths(PathSelectors.any())
				.build();
	}
}

SwaggerController Class Definition

package com.tuts.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/swagger")
@Api("Configure The Swagger With 247 Tuts")
public class SwaggerController {
	@GetMapping("/hello")
	@ApiOperation(value="return hello using map")
	public Map<String, String> helloSwggaer(){
		Map<String, String> map=new HashMap<String, String>();
		map.put("message", "Hello Swagger");
		return map;
	}
}

Download Whole Code On Provided Link
https://github.com/247tuts/247TutsSwagger

Raman

Related post

Leave a Reply

Your email address will not be published. Required fields are marked *