subham sharma
subham sharma

Reputation: 13

Request method 'POST' not supported description The specified HTTP method is not allowed for the requested resource

i am getting this "message Request method 'POST' not supported

description The specified HTTP method is not allowed for the requested resource. "

and

my controller method is:-

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

    }   

Upvotes: 0

Views: 1113

Answers (1)

Alexander Molkov
Alexander Molkov

Reputation: 61

You're using RequestMethod.GET for addToCart method.

  • change request mapping to: @RequestMapping(value = "/addtocart/{id}", method = RequestMethod.POST)
  • leave as is and use GET request to call your method

Upvotes: 1

Related Questions