Spring Boot and Swagger 3 Example

In this example we will use the Springfox implementation fo the swagger 2 specification with Spring boot.
What is swagger?
Swagger is used for visualizing ReST APIs, it provides online sandbox for the consumers or developers. Once swagger visualization implemented consumers can understand and check the service without any documentation.
This simple example says how to implement swagger in a spring boot application
1. Create a spring boot application and add below dependencies
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.vinod.test</groupId>
 <artifactId>springboot-swagger-test</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.1.RELEASE</version>
 </parent>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.2.2</version>
  </dependency>

  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.2.2</version>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>
2. Create a spring boot configuration class and enable Swagger and creates beans
package com.vinod.test;

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

import com.google.common.base.Predicate;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;
import static com.google.common.base.Predicates.or;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

 @Bean
 public Docket postsApi() {
 return new Docket(DocumentationType.SWAGGER_2).groupName("public-api")
 .apiInfo(apiInfo()).select().paths(postPaths()).build();
 }

 private Predicate<String> postPaths() {
 return or(regex("/api/posts.*"), regex("/api/test.*"));
 }

 private ApiInfo apiInfo() {
 return new ApiInfoBuilder().title("SWAGGER API")
 .description("SWAGGER SIMPLE EXAMPLE")
 .termsOfServiceUrl("http://vinodkkumaran.blogspot.com/")
 .contact("kkvinod.kumaran@gmail.com").license("KK VINOD License")
 .licenseUrl("kkvinod.kumaran@gmail.com").version("1.0").build();
 }

}
3) Create a simple ReST controller class
package com.vinod.test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

 @RequestMapping(method = RequestMethod.GET, value = "/api/test")
 public String sayHelloWorld() {
 return "Hello world from Swagger Example";
 }
}
4) Create spring boot main class
package com.vinod.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySwaggerApplication {

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

 }

}
Done.. run it..

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

No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts