JPA控制器更新功能

时间:2017-08-28 14:26:54

标签: angularjs spring-boot spring-data-jpa spring-rest

  

错误消息:找不到符号
    符号:方法saveAndFlash(产品)
    location:ProductRepository类型的变量存储库

我想使用JpaRepository

执行更新
@RestController
public class ProductController {

        @Autowired
        private ProductRepository repository;

        @RequestMapping("/product/{productId}")
        public Product getProduct(@PathVariable long id) {
            return repository.findOne(id);
        }

        @RequestMapping(method = RequestMethod.PUT, value = "/update")
        public Product updateProduct(@RequestBody Product product, @PathVariable long id) {
            product.setId(id);
            return repository.saveAndFlash(product);
        }
}

1 个答案:

答案 0 :(得分:0)

@RestController
@RequestMapping("/products")
public class ProductController {

        @Autowired
        private ProductRepository repository;

        @GetMapping("/{id}") // GET /products/1
        public Product get(@PathVariable("id") long id) {
            return repository.findOne(id);
        }

        @PutMapping("/{id}") // PUT /products/1
        public Product update(@RequestBody Product product, @PathVariable("id") long id) {
            product.setId(id);
            return repository.saveAndFlush(product);
        }
}