Spring MVC 如何解决不支持的请求方法'GET'

u3r8eeie  于 2022-11-15  发布在  Spring
关注(0)|答案(3)|浏览(216)

我的情况低于警戒线
WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-1) Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]
我已经将方法设置为POST,但仍然收到上述错误。我收到了删除控制器的警告消息,除了删除之外,所有其他CRUD操作都正常工作。
请查找以下代码控制器Map的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>

delete方法调用的DAO实现:

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]
k4ymrczo

k4ymrczo1#

我不明白@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

31moq8wy

31moq8wy2#

因为我认为请求表单会使用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>
disbfnqx

disbfnqx3#

编写下面的代码后

@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上尝试过,但没有收到错误。

相关问题