如何解决不支持请求方法“ GET”的问题

时间:2019-01-26 05:56:45

标签: spring hibernate spring-mvc web-applications

我的警告低于

WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-1) Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]

我已经将方法设置为POST,但仍然遇到上述错误。我的删除控制器收到此警告消息,除删除外,所有其他CRUD操作均正常工作。

请找到以下代码 控制器映射的deleteproducts:

@RequestMapping(value="/deleteproducts", method= RequestMethod.POST)
    public String deleteProduct(@PathVariable("productId")int productId) {
        IProductsDAO ip = new ProductsDAOImpl();
        boolean b = ip.deleteProduct(productId);
        if(b)
            return "success";
        else
            return "deleteproducts";

这是我的jsp视图:

<body>
<form id="update product form" action="${pageContext.request.contextPath}/deleteproducts" method="post" role="form" style="display: none;">
    <div class="form-group row">
        <label for="product Id" class="col-sm-2 col-form-label">Id</label>
        <div class="col-sm-10">
            <input type="text" name="productId" class="form-control" id="productid" placeholder="Enter the product Id you want to delete">
        </div>
    </div>  
</form>
</body>

DAOimplementation用于删除方法调用:

public boolean deleteProduct(int productId)
    {
        boolean b = true;
        try
        {
            sess.beginTransaction();
            Products p = (Products)sess.load(Products.class, new Integer(productId));
            sess.delete(p);
            sess.getTransaction().commit();
        }catch(Exception ex)
        {
            sess.getTransaction().rollback();
            b = false;
        }
        return b;
    }

现在有人可以告诉我应该对我的代码进行哪些更改以解决此问题吗?

谢谢!

编辑1:

@DeleteMapping(value="/deleteproducts/{productId}")
    public String deleteProduct(@PathVariable("productId")int productId) {
        IProductsDAO ip = new ProductsDAOImpl();
        boolean b = ip.deleteProduct(productId);
        if(b)
            return "success";
        else
            return "deleteproducts";
    }

仍然收到以下警告:

WARN  [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-1) Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]

3 个答案:

答案 0 :(得分:0)

我不了解@RequestMapping(value="/deleteproducts", method= RequestMethod.POST) 这是什么意思您要让RequestMapping删除记录,方法是POST吗?

我建议请遵循标准的开发方式。如果要删除DeleteMapping,则对于POST,请使用PostMapping,而要检索某些信息,则可以使用GetMapping。

理想情况下应该是

@DeleteMapping("/deleteproducts/{id}") public void deleteStudent(@PathVariable long id) { deleteproductsRepository.deleteById(id); or some CRUD logic to delete }

您可以参考此链接以更好地了解REST

答案 1 :(得分:0)

我认为请求表单将转到GET方法,因此您可以尝试使用JavaScript来通过函数调用提交表单。

请找到以下代码:

 <form id="update product form" action="${pageContext.request.contextPath}/deleteproducts"  method="POST">
  <div class="form-group row">
     <label for="product Id" class="col-sm-2 col-form-label">Id</label>
        <div class="col-sm-10">
        <input type="text" name="productId" class="form-control" id="productid" placeholder="Enter the product Id you want to delete">
         </div>
        <div class="col-sm-10">
         <input type="button"  value="submit" onclick="javascript:formSubmit()" name="submit" ></a>
        </div>
       </div>
 </form>



 <script>
    function formSubmit() {
if(!isEmpty(document.from.productId.value)){ //even you can validate values in productId 
    document.form.method='POST';
    document.form.action='/deleteproducts';
    document.form.submit();
}
    )
    <script>

答案 2 :(得分:0)

编写以下代码后

@DeleteMapping(value="/deleteproducts/{productId}")
    public String deleteProduct(@PathVariable("productId")int productId) {
        IProductsDAO ip = new ProductsDAOImpl();
        boolean b = ip.deleteProduct(productId);
        if(b)
            return "success";
        else
            return "deleteproducts";
    }

此后,请不要在常规浏览器上运行,请尝试在REST API上运行它。我在POSTMAN API上尝试过,但未收到错误。看下面的图片

enter image description here