Merge pull request #11863 from sanitaso/master
BAEL-5375 Set List of Objects in Swagger API response
This commit is contained in:
commit
2695ba0fde
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue