请求方法' POST'不支持的描述所请求的资源

时间:2017-04-20 18:52:25

标签: java maven

我收到此消息"消息请求方法' POST'不支持

description请求的资源不允许使用指定的HTTP方法。 "

我的控制器方法是: -

@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;

    }   

1 个答案:

答案 0 :(得分:1)

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

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