gpt4 book ai didi

java - 请求方法 'POST' 不支持 说明 所请求的资源不允许指定的 HTTP 方法

转载 作者:行者123 更新时间:2023-12-02 13:22:01 27 4
gpt4 key购买 nike

我收到此消息“不支持请求方法‘POST’”

描述 所请求的资源不允许指定的 HTTP 方法。”

我的 Controller 方法是:-

@RequestMapping(value = "/addtocart/{id}", method = RequestMethod.GET)
public ModelAndView addToCart(@PathVariable("id") String id) {
log.debug("Starting of the method addToCart");
// get the product based on product id
Product product = productDAO.getProductBYID(id);
cart.setPrice(product.getPrice());
cart.setProductName(product.getName());
String loggedInUserid = (String) session.getAttribute("loggedInUserID");
if (loggedInUserid == null) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
loggedInUserid = auth.getName();
}
cart.setUserID(loggedInUserid);
//It is not required if you given default value while creating the table
cart.setStatus('N'); // Status is New. Once it is dispatched, we can
// changed to 'D'

//To get sequence number, you can do programmatically in DAOImpl
//myCart.setId(ThreadLocalRandom.current().nextLong(100, 1000000 + 1));


cartDAO.save(cart);
// return "redirect:/view/Home.jsp";

ModelAndView mv = new ModelAndView("redirect:/Home");
mv.addObject("successMessage", " Successfuly add the product to myCart");
log.debug("Ending of the method addToCart");
return mv;

}

最佳答案

您正在使用 RequestMethod.GET 作为 addToCart 方法。

  • 将请求映射更改为:@RequestMapping(value = "/addtocart/{id}", method = RequestMethod.POST)
  • 保持原样并使用 GET 请求调用您的方法

关于java - 请求方法 'POST' 不支持 说明 所请求的资源不允许指定的 HTTP 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43527651/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com