Cloud native 与 Spring Cloud 最佳实践

Cloud native 与 Spring Cloud_入门_4_CRUD之Controller

编写 Controller 文件
@RestController
@RequestMapping("/api/v1/products")
@Tag(name = "Product")
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping
    @Operation(summary = "Get a paginated product list. An optional keyword can be provided to narrow the search.")
    public PageInfo<Product> list(@Valid ProductSearchInput input) {
        PageInfo<Product> data = productService.search(input);
        return data;
    }

    @GetMapping("/{id}")
    @Operation(summary = "Get detailed information about the product.")
    public Product show(@PathVariable Integer id) {
        Product record = productService.findById(id);
        return record;
    }

    @PostMapping
    @Operation(summary = "Create a new product.")
    public Integer create(@Valid @RequestBody ProductInput record) {
        Integer id = productService.create(record);
        return id;
    }

    @PutMapping("/{id}")
    @Operation(summary = "Update an existing product.")
    public void update(@PathVariable Integer id, @RequestBody ProductInput record) {
        InetUtils.HostInfo host = new InetUtils.HostInfo();
        log.info("server=> {}:{}", id, host.getHostname(), host.getIpAddress());
        log.info("updating product, id: {}, ", id);

        productService.update(id, record);
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "Delete one or more products by id. Multiple ids can be separated with comma.")
    public void delete(@PathVariable String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        productService.deleteByIds(idList);
    }

    @GetMapping("/export-csv")
    @Operation(summary = "Export products to a cvs file.")
    public ResponseEntity<InputStreamResource> exportCsv(@Valid ProductSearchInput input) {
        return productService.exportCsv(input);
    }

    @PostMapping(value = "/import-csv", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "Import products from a cvs file.")
    public void importCsv(@RequestPart(value = "file", required = false) final MultipartFile file) throws Exception {
        productService.importCsv(file.getInputStream());
    }

}