From 618cdbdcedd77fb6690b151c8d53d0d587337a5d Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 19 Jun 2021 13:29:50 +0430 Subject: [PATCH] BAEL-4956: add source code for describng REST API in openapi --- .../com/baeldung/tagopenapi/Application.java | 12 +++++++++++ .../baeldung/tagopenapi/config/OpenApi.java | 20 +++++++++++++++++++ .../tagopenapi/controller/BookController.java | 20 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/Application.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/config/OpenApi.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/controller/BookController.java diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/Application.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/Application.java new file mode 100644 index 0000000000..3141428514 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.tagopenapi; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication() +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/config/OpenApi.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/config/OpenApi.java new file mode 100644 index 0000000000..780b202d3c --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/config/OpenApi.java @@ -0,0 +1,20 @@ +package com.baeldung.tagopenapi.config; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; + +public class OpenApi { + + @Bean + public OpenAPI customOpenAPI(@Value("${springdoc.version}") String appVersion) { + return new OpenAPI().info(new Info().title("Controller API") + .version(appVersion) + .description("This is a sample server created using springdocs - a library for OpenAPI 3 with spring boot.") + .termsOfService("http://swagger.io/terms/") + .license(new License().name("Apache 2.0") + .url("http://springdoc.org"))); + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/controller/BookController.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/controller/BookController.java new file mode 100644 index 0000000000..3c81c1cee7 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/tagopenapi/controller/BookController.java @@ -0,0 +1,20 @@ +package com.baeldung.tagopenapi.controller; + +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@RestController +@RequestMapping("/api/book") +@Tag(name = "book service", description = "the book API with description tag annotation") +public class BookController { + + @GetMapping("/") + public List getBooks() { + return Arrays.asList("book1", "book2"); + } +}