首页>代码>Spring Boot整合VueJS实现对mysql数据库增删改查的入门实例>/springboot-crud-mysql-vuejs/src/main/java/com/hellokoding/springboot/restful/product/ProductAPI.java
package com.hellokoding.springboot.restful.product; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; import java.util.Optional; @RestController @RequestMapping("/api/v1/products") @Slf4j @RequiredArgsConstructor public class ProductAPI { private final ProductService productService; @GetMapping public ResponseEntity<List<Product>> findAll() { return ResponseEntity.ok(productService.findAll()); } @PostMapping public ResponseEntity create(@Valid @RequestBody Product product) { return ResponseEntity.ok(productService.save(product)); } @GetMapping("/{id}") public ResponseEntity<Product> findById(@PathVariable Long id) { Optional<Product> stock = productService.findById(id); if (!stock.isPresent()) { log.error("Id " + id + " is not existed"); ResponseEntity.badRequest().build(); } return ResponseEntity.ok(stock.get()); } @PutMapping("/{id}") public ResponseEntity<Product> update(@PathVariable Long id, @Valid @RequestBody Product product) { if (!productService.findById(id).isPresent()) { log.error("Id " + id + " is not existed"); ResponseEntity.badRequest().build(); } return ResponseEntity.ok(productService.save(product)); } @DeleteMapping("/{id}") public ResponseEntity delete(@PathVariable Long id) { if (!productService.findById(id).isPresent()) { log.error("Id " + id + " is not existed"); ResponseEntity.badRequest().build(); } productService.deleteById(id); return ResponseEntity.ok().build(); } }

543666826 LV34
2024年12月28日
123456YQQ LV14
2023年9月28日
lingtiejing LV15
2023年7月16日
微信网友_6417118785228800 LV3
2023年4月4日
qazwsx0987 LV5
2023年3月27日
hbsoft2008 LV16
2023年2月17日
abdkfksdkf LV16
2023年2月4日
yjCASDAS LV3
2022年11月30日
好的好的 LV8
2022年11月21日
chengqiang LV13
2022年7月13日

543666826 LV34
2024年12月28日
微信网友_6955249237250048 LV5
2024年12月28日
ma406805131 LV19
2024年12月19日
dengge123 LV14
2024年12月13日
au614webcreate
2024年6月25日
暂无贡献等级
计科一班 LV7
2024年6月19日
chenranr LV10
2024年6月15日
szf123 LV12
2024年5月29日
cccccc1235
2024年5月20日
暂无贡献等级
1941549176 LV4
2024年5月10日