我的Spring Boot项目中的类控制器中的注释存在问题。我应该使用什么方法?
@Controller
@RequestMapping("/products")
public class ProductController {
/**
* Only a Test
*/
@Autowired
private ProductService productService;
答案 0 :(得分:0)
您可能至少应该具有CRUD方法,诸如此类:
@GetMapping
public String index(Model model) {
List<Product> products = productService.searchAll();
model.addAttribute("products", products);
return "product/index";
}
@GetMapping("/addProduct")
public String add(Product product, Model model) {
logger.info("Creating new 'Product' on data source");
this.productService.inserir(product);
return "product/addProduct";
}
@GetMapping("/editProduct/{id}")
public String edit(@PathVariable("id") Long id, Model model) {
logger.info("Find 'Product' Id: {} on data source", id);
Product product = this.productService.searchById(id);
model.addAttribute("product", product);
return "product/editProduct";
}
@GetMapping("/deleteProduct/{id}")
public String delete(@PathVariable("id") Long id, Model model) {
logger.info("Deleting 'Product' Id:{} from data source", id);
this.productService.deletar(id);
return index(model);
}
@PostMapping("/saveProduct")
public String save(@Valid Product product, BindingResult result, Model model) {
if (result.hasErrors()) {
return add(product, model);
}
if (product.getId() != 0) {
logger.info("Updating 'Product' Id: {} on data source", product.getId());
this.productService.atualizar(product);
} else {
logger.info("Creating new 'Product' on data source");
this.productService.inserir(product);
}
return index(model);
}