Merge pull request #11863 from sanitaso/master

BAEL-5375 Set List of Objects in Swagger API response
This commit is contained in:
davidmartinezbarua 2022-03-13 11:11:42 -03:00 committed by GitHub
commit 2695ba0fde
4 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package com.baeldung.swaggerresponseapi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SwaggerResponseApiApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerResponseApiApplication.class, args);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.swaggerresponseapi.controller;
import com.baeldung.swaggerresponseapi.model.Product;
import com.baeldung.swaggerresponseapi.service.ProductService;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ProductController {
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@PostMapping("/create")
public Product addProduct(@RequestBody Product product) {
return productService.addProducts(product);
}
@ApiResponses(value = { @ApiResponse(content = { @Content(mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = Product.class))) }) })
@GetMapping("/products")
public List<Product> getProductsList() {
return productService.getProductsList();
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.swaggerresponseapi.model;
public class Product {
String code;
String name;
public Product(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.swaggerresponseapi.service;
import com.baeldung.swaggerresponseapi.model.Product;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ProductService {
List<Product> productsList = new ArrayList<>();
public Product addProducts(Product product) {
productsList.add(product);
return product;
}
public List<Product> getProductsList() {
return productsList;
}
}