2017-04-20 363 views
0

我得到這個「消息請求方法‘POST’不支持請求方法「POST」不支持的描述指定的HTTP方法是不允許請求的資源

描述指定的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; 

    } 
+0

的[這]可能的複製(http://stackoverflow.com/questions/11145884/HTTP-STATUS-405-請求方法-後未負載型彈簧MVC)。 –

+0

你想用POST來調用這個API嗎?方法類型應該是基於控制器的GET – gwcoderguy

+0

如何調用此方法?你的網址是什麼樣的? – yogidilip

回答

1

您使用RequestMethod.GETaddToCart方法。

  • 變更請求映射到: @RequestMapping(value = "/addtocart/{id}", method = RequestMethod.POST)
  • 保留原樣,並使用GET要求打電話給你的方法
相關問題