已解决 [org.springframework.web.HttpRequestMethodNotSupportedException: 不支持请求方法'POST'] Spring MVC

Posted

技术标签:

【中文标题】已解决 [org.springframework.web.HttpRequestMethodNotSupportedException: 不支持请求方法\'POST\'] Spring MVC【英文标题】:Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported] Spring MVC已解决 [org.springframework.web.HttpRequestMethodNotSupportedException: 不支持请求方法'POST'] Spring MVC 【发布时间】:2022-01-15 23:17:26 【问题描述】:

我使用 Spring Mvc、Hibernate、Maven 创建了一个示例 crud 操作应用程序。我所有的操作都运行良好。我在更新方法上有问题。我不知道我哪里出错了。我参考了这篇文章。 https://java2blog.com/spring-mvc-hibernate-mysql-crud-example/

添加、删除、显示方法工作正常。 这是我尝试更新时遇到的问题。 警告:已解决 [org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“POST”] 我不知道怎么了。请帮忙 output

控制器类:

package com.spring.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.bean.Person;
import com.spring.service.PersonService;

@Controller
public class PersonController 

    @Autowired
    PersonService service;
    
    @RequestMapping(value = "/getAllPersons", method = RequestMethod.GET, headers = "Accept=application/json")
    public String getPerson(Model model) 
 
        List<Person> listOfPersons = service.getAllPersons();
        model.addAttribute("person", new Person());
        model.addAttribute("listOfPersons", listOfPersons);
        return "PersonDetails";
    
    
    @RequestMapping(value = "/getPerson/id", method = RequestMethod.GET, headers = "Accept=application/json")
    public Person getPerson(@PathVariable int id) 
        return service.getPerson(id);
    
    

    @RequestMapping(value = "/addPerson", method = RequestMethod.POST, headers = "Accept=application/json")
    public String addPerson(@ModelAttribute("person") Person person) 

        if(person.getId()==0) 
            service.addPerson(person);
        
        else 
            service.updatePerson(person);
        
        
        return "redirect:/getAllPersons";
    
    
    //@RequestMapping(value = "/updateCountry/id", method = RequestMethod.GET, headers = "Accept=application/json")
    @RequestMapping(value = "/updatePerson/id", method = RequestMethod.GET, headers = "Accept=application/json")
    public String updatePerson(@PathVariable("id") int id,Model model) 
        model.addAttribute("person", this.service.getPerson(id));
        model.addAttribute("listOfPersons", this.service.getAllPersons());
        return "PersonDetails";
    
 
    @RequestMapping(value = "/deletePerson/id", method = RequestMethod.GET, headers = "Accept=application/json")
    public String deletePerson(@PathVariable("id") int id) 
        service.deletePerson(id);
        return "redirect:/getAllPersons";
    

道类:

package com.spring.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.spring.bean.Person;

@Repository
public class PersonDao 
     
    @Autowired
    private SessionFactory sessionfactory;
    
    public void setSessionFactory(SessionFactory sf) 
        this.sessionfactory = sf;
    
    
    public List<Person> getAllPersons() 
        Session session=this.sessionfactory.getCurrentSession();
        
        @SuppressWarnings("unchecked")
        List<Person> persons= session.createQuery("from Person").list();
        
        for(Person p:persons) 
            System.out.print(p.getName()+" "+p.getCountry());
        
        
        return persons;
        
    
    
    public Person getPerson(int id) 
        Session session=this.sessionfactory.getCurrentSession();
        System.out.println("Here");
        Person p=(Person)session.load(Person.class, new Integer(id));
        System.out.println(p);
        return p;
    
    
    public Person addPerson(Person person) 
        Session session=this.sessionfactory.getCurrentSession();
        
        session.persist(person);
        return person;
    
    
    public void updatePerson(Person person) 
        Session session=this.sessionfactory.getCurrentSession();
        
        session.update(person);
    
    
    public void deletePerson(int id) 
        Session session=this.sessionfactory.getCurrentSession();
        
        Person p=session.load(Person.class, new Integer(id));
        if(null!= p) 
            session.delete(p);
        
        
    

Jsp 页面:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Person Details</title>
</head>
<body>
<form:form method="post" modelAttribute="person" action="addPerson">
<table>
        <tr>
            <th colspan="2">Add Person</th>
        </tr>
        <tr>
        <form:hidden path="id" />
        </tr>
        <tr>
          <td><form:label path="name">Name:</form:label></td>
          <td><form:input path="name" size="30" maxlength="30"></form:input></td>
        </tr>
        <tr>
                <td><form:label path="country">Country:</form:label></td>
          <td><form:input path="country" size="30" maxlength="30"></form:input></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit"
                class="blue-button" /></td>
        </tr>
    </table>
</form:form>
<hr>
  <h3>Person List</h3>
<c:if test="$!empty listOfPersons">
    <table class="tg">
    <tr>
        <th >Id</th>
        <th >Person Name</th>
        <th >Country</th>
        <th >Edit</th>
        <th >Delete</th>
    </tr>
    <c:forEach items="$listOfPersons" var="person">
        <tr>
            <td>$person.id</td>
            <td>$person.name</td>
            <td>$person.country</td>
            <td><a href="<c:url value='updatePerson/$person.id' />" >Edit</a></td>
            <td><a href="<c:url value='deletePerson/$person.id' />" >Delete</a></td>
        </tr>
    </c:forEach>
    </table>
</c:if>
</body>
</html>

【问题讨论】:

【参考方案1】:

您的端点只接受GET 请求。您需要将其更改为POST,如下所示:

@RequestMapping(value = "/updatePerson/id", method = RequestMethod.POST, 
                headers = "Accept=application/json")
public String updatePerson(@PathVariable("id") int id,Model model) 
    model.addAttribute("person", this.service.getPerson(id));
    model.addAttribute("listOfPersons", this.service.getAllPersons());
    return "PersonDetails";

尽管如此,您应该考虑使用PUT 进行更新请求,以便您遵循标准。你可以在https://www.restapitutorial.com/lessons/httpmethods.html阅读更多内容。

【讨论】:

我做了这个改变。但我得到一个例外,即不支持 GET 请求。我还将 POST 和 GET 放在一起。仍然出现错误。 这毫无意义。您最初的错误是Request method 'POST' not supported,我建议您按照上面的方式更改代码,现在您得到了相同的结果,但GET?您是否在正确的位置更改了代码?您是否在其他任何地方调用端点? Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]......我得到了这个异常,是的,我做了和你提到的一样的改变...... . 但是你在哪里用GET 调用端点/updatePerson/id?您必须将其称为POST 请求... 是的,我在控制器中做到了。将方法类型更改为发布。但后来我得到了上述异常。

以上是关于已解决 [org.springframework.web.HttpRequestMethodNotSupportedException: 不支持请求方法'POST'] Spring MVC的主要内容,如果未能解决你的问题,请参考以下文章

已解决:org.springframework.cloud.client.discovery.EnableDiscoveryClient 不存在

org.springframework.context.ApplicationContextException Unable to start web server报错(已解决)

已解决:SpringBoot启动报错:Unable to start web server; nested exception is org.springframework.boot.web

org.springframework.web.context.ContextLoaderListen 报错解决办法

解决: org.springframework.beans.factory.BeanNotOfRequiredTypeException

学习黑马教学视频SSM整合中Security遇到的问题org.springframework.security.access.AccessDeniedException: Access is deni