(type=Bad Request,status=400)方法参数类型long所需的请求参数'productId'不存在(Spring MVC - Hibernate JPA)

iklwldmw  于 2023-10-23  发布在  Spring
关注(0)|答案(2)|浏览(157)

有一个关于jpa休眠(crud操作)的问题,实际上我可以做创建,读取和删除操作,但当我遇到更新操作时,我不知道我应该做什么。这是我的代码。更新操作有问题。你能帮帮我吗?

@Autowired
ProductRepository productRepository;

//**************WELCOME***************
@GetMapping("/welcome")
public String welcomePage() {
    return "welcome";
}

// **************İNSERT****************
@GetMapping(path = { "/productinsert"})
public String insertProduct(Model model) {
    Product product = new Product(" ", 0.0); // Gösterim Katmanına ilk boş bir sayfa gitmeli o yüzden değerler boş
    model.addAttribute("product", product);
    return "/productinsert";
}

@PostMapping("/productinsert")
public String insertProduct(Product product, BindingResult bindingResult, Model model) {
    productRepository.save(product);
    if (bindingResult.hasErrors()) {
        return "/productinsert";
    } else {
        model.addAttribute("mesaj", "Ürününüz Başarıyla Kaydedilmiştir.");
        return "/productsuccess";
    }
}

//************UPDATE**************
@GetMapping("/productedit")
public String editProduct() {
    return "/productedit";
}
***//PROBLEM İS HERE//***
@RequestMapping(value = "/productedit", method = RequestMethod.POST)
public String editProductt(@RequestParam long productId, Model model) {
    Optional<Product> proOptional =  productRepository.findById(productId);
    Product product = proOptional.get();
    model.addAttribute("product", product);
    return "/productinsert";
}

}
问题在于
出现意外错误(类型=错误请求,状态=400)。方法参数类型long的所需请求参数“productId”不存在org.springframework.web.bind.MissingServletRequestParameterException:方法参数类型long所需的请求参数“productId”不存在

pbossiut

pbossiut1#

此错误意味着您的请求中缺少productId参数,请求格式必须为https://your-domain:port/productedit?productId=X,例如:https://localhost:8080/productedit?productId=1

uoifb46i

uoifb46i2#

使用了@RequestParam注解使用了:

@GetMapping("/fetchData) 
public Employee FindById(@RequestParam int id) {
    Optional<Employee> optional = er.findById(id);
    Employee employee = optional.get();
    return employee;
}

相关问题