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 <Abhinav2.Pandey@airtel.com>
This commit is contained in:
parent
3f46e530e7
commit
ed09368f21
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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<Void> 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<Product> 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"));
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue