From ed09368f21ea9fac5eea77db3b86f41466552d00 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Mon, 7 Feb 2022 11:37:48 +0530 Subject: [PATCH] BAEL-5327 - Set a description and an example in Swagger (#11749) * BAEL-5327 - Set a description and an example in Swagger * BAEL-5327 - two space indentation for line continuation Co-authored-by: Abhinav Pandey --- .../SwaggerExampleApplication.java | 13 ++++++ .../swaggerexample/config/SwaggerConfig.java | 38 +++++++++++++++ .../controller/ProductController.java | 34 ++++++++++++++ .../swaggerexample/entity/Product.java | 46 +++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/SwaggerExampleApplication.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/config/SwaggerConfig.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/controller/ProductController.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/entity/Product.java diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/SwaggerExampleApplication.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/SwaggerExampleApplication.java new file mode 100644 index 0000000000..4859224bb2 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/SwaggerExampleApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerexample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SwaggerExampleApplication { + + public static void main(String[] args) { + SpringApplication.run(SwaggerExampleApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/config/SwaggerConfig.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/config/SwaggerConfig.java new file mode 100644 index 0000000000..32dbe87bae --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/config/SwaggerConfig.java @@ -0,0 +1,38 @@ +package com.baeldung.swaggerexample.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +import java.util.Collections; + +@Configuration +@EnableWebMvc +public class SwaggerConfig { + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.baeldung.swaggerexample")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfo( + "Products API", + "API to let you add and view product", + "0.0.1", + "Terms of service", + new Contact("John Doe", "www.example.com", "myemail@company.com"), + "License of API", "API license URL", Collections.emptyList()); + } +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/controller/ProductController.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/controller/ProductController.java new file mode 100644 index 0000000000..7500c01bc0 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/controller/ProductController.java @@ -0,0 +1,34 @@ +package com.baeldung.swaggerexample.controller; + +import com.baeldung.swaggerexample.entity.Product; +import io.swagger.annotations.*; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@ApiOperation("Products API") +public class ProductController { + + @ApiOperation(value = "Create a new product", notes = "Creates a new product as per the request body") + @ApiResponses(value = { + @ApiResponse(code = 201, message = "Successfully created"), + @ApiResponse(code = 400, message = "Bad request - The product is not valid"), + @ApiResponse(code = 500, message = "Internal server error - Something went wrong") + }) + @PostMapping(value = "/products") + public ResponseEntity createProduct(@RequestBody Product product) { + return new ResponseEntity<>(HttpStatus.CREATED); + } + + @ApiOperation(value = "Get a product by id", notes = "Returns a product as per the id") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved"), + @ApiResponse(code = 404, message = "Not found - The product was not found") + }) + @GetMapping("/products/{id}") + public ResponseEntity getProduct(@PathVariable("id") @ApiParam(name = "id", value = "Product id", example = "1") Long id) { + //retrieval logic + return ResponseEntity.ok(new Product(1, "Product 1", "$21.99")); + } +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/entity/Product.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/entity/Product.java new file mode 100644 index 0000000000..122addadd5 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/entity/Product.java @@ -0,0 +1,46 @@ +package com.baeldung.swaggerexample.entity; + +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; + +public class Product implements Serializable { + @ApiModelProperty(notes = "Product ID", example = "1", required = true) + private Long id; + @ApiModelProperty(notes = "Product name", example = "Product 1", required = false) + private String name; + @ApiModelProperty(notes = "Product price", example = "$100.00", required = true) + private String price; + + // constructor and getter/setters + + public Product(long id, String name, String price) { + this.id = id; + this.name = name; + this.price = price; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } +}