Java 更新操作未执行-已解析[org.springframework.web.HttpRequestMethodNotSupportedException:请求方法';POST';不受支持]

Java 更新操作未执行-已解析[org.springframework.web.HttpRequestMethodNotSupportedException:请求方法';POST';不受支持],java,spring,hibernate,spring-mvc,spring-data-jpa,Java,Spring,Hibernate,Spring Mvc,Spring Data Jpa,我试图在spring boot+thymeleaf中开发一个应用程序,并且我能够从MySQL数据库的profile选项卡中检索登录的用户详细信息,但是当我尝试更改一个或两个字段详细信息(更新)并点击update按钮时,会显示一条错误消息-Fri Sep 04 20:39:47 IST 2020 出现意外错误(类型=方法不允许,状态=405)。 不支持请求方法“POST” 查看我的控制器代码(我使用在类顶部注释的@RestController)- 叶子代码(html)- 配置文件详细信息 我希

我试图在spring boot+thymeleaf中开发一个应用程序,并且我能够从MySQL数据库的profile选项卡中检索登录的用户详细信息,但是当我尝试更改一个或两个字段详细信息(更新)并点击update按钮时,会显示一条错误消息-Fri Sep 04 20:39:47 IST 2020 出现意外错误(类型=方法不允许,状态=405)。 不支持请求方法“POST”

查看我的控制器代码(我使用在类顶部注释的@RestController)-

叶子代码(html)-


配置文件详细信息

我希望当用户登录和访问时,他/她应该能够检查他的/她的个人资料(我能够执行工作代码),当用户想要更新几个字段(根据选择1-2)并点击更新时,他/她应该能够更新详细信息(不创建新用户或记录)因为当我在类的顶部使用@Controller时,这段代码工作并创建新的用户,而不是更新。

您的控制器被注释为
@RequestMapping(value=“/profile”,method=RequestMethod.PUT)
,这使它成为一个PUT端点。然而,你的要求显然是一个职位。如果我们查看您的html表单,它包含
method=“post”
。HTML表单只支持GET和POST作为有效方法,因此您需要将端点更新为POST端点

tldr


您的控制器用
@RequestMapping(value=“/profile”,method=RequestMethod.PUT)
注释,这使其成为PUT端点。然而,你的要求显然是一个职位。如果我们查看您的html表单,它包含
method=“post”
。HTML表单只支持GET和POST作为有效方法,因此您需要将端点更新为POST端点

tldr


您在is POST中请求映射,但控制器已设置为接受请求作为PUT

<form name="myForm" th:action="@{/profile}" th:object="${customer}" **method="post"**>


@RequestMapping(value = "/profile", method = **RequestMethod.PUT**) 

@RequestMapping(value=“/profile”,method=**RequestMethod.PUT**)

只需以类似的方式保持它们,两者应该是相同的。

您在is POST中请求映射,但控制器已设置为接受请求为PUT

<form name="myForm" th:action="@{/profile}" th:object="${customer}" **method="post"**>


@RequestMapping(value = "/profile", method = **RequestMethod.PUT**) 

@RequestMapping(value=“/profile”,method=**RequestMethod.PUT**)

请以相似的方式保存这些内容,两者应该相同。

请检查我发现的内容并解决此问题

@RequestMapping(value = "/profile" ,method = RequestMethod.POST)
    public ModelAndView updateProfile(@ModelAttribute Customer customer, HttpSession session) {
        
        ModelAndView model = new ModelAndView();
        
        Customer exist = cRepo.findByCustEmail(customer.getCustEmail());

        if(exist != null) {
            if(exist.getCustEmail().equals(session.getAttribute("emailsession"))) {
                
                **exist.setCustId(exist.getCustId());
                exist.setCustName(customer.getCustName());
                exist.setCustEmail(customer.getCustEmail());
                exist.setCustPassword(customer.getCustPassword());**
                
                cRepo.save(exist);
                model.addObject("msg", "User Details has been successfully updated!!");
                
                model.addObject("customer", exist);
                model.setViewName("profile");
            }
                
        }else {
            
            model.addObject("exist", "Please enter correct email address!");
            String email = (String) session.getAttribute("emailsession");
            Customer cust = cRepo.findByCustEmail(email);
            model.addObject("customer", cust);
            model.setViewName("profile");
            
        }
        
        return model;
    }

请检查我的发现并解决此问题

@RequestMapping(value = "/profile" ,method = RequestMethod.POST)
    public ModelAndView updateProfile(@ModelAttribute Customer customer, HttpSession session) {
        
        ModelAndView model = new ModelAndView();
        
        Customer exist = cRepo.findByCustEmail(customer.getCustEmail());

        if(exist != null) {
            if(exist.getCustEmail().equals(session.getAttribute("emailsession"))) {
                
                **exist.setCustId(exist.getCustId());
                exist.setCustName(customer.getCustName());
                exist.setCustEmail(customer.getCustEmail());
                exist.setCustPassword(customer.getCustPassword());**
                
                cRepo.save(exist);
                model.addObject("msg", "User Details has been successfully updated!!");
                
                model.addObject("customer", exist);
                model.setViewName("profile");
            }
                
        }else {
            
            model.addObject("exist", "Please enter correct email address!");
            String email = (String) session.getAttribute("emailsession");
            Customer cust = cRepo.findByCustEmail(email);
            model.addObject("customer", cust);
            model.setViewName("profile");
            
        }
        
        return model;
    }


当我更改为RequestMapping(value=“/profile”,method=RequestMethod.POST)时,它正在数据库中添加新记录(新用户)。但是我想更新细节,因为在profile选项卡中,用户应该更新细节,而不是添加新的。您现在可以提出建议吗?这与您现有的问题无关,但通常要做的是将表的主键传递给端点,并使用它来确定是否需要执行插入或更新。如果主键不为null,您知道它应该是一个更新,反之亦然,我的问题既是一个错误,也是一个更新查询,因为我正在进行更新,并将表的正确主键设置为notnull,这意味着主键已经存在于数据库中,因此应该覆盖它,而不是创建新的主键。你能推荐我吗?我是新手,正在寻求帮助。您能帮助我吗?当我更改为RequestMapping(value=“/profile”,method=RequestMethod.POST)时,它正在数据库中添加新记录(新用户)。但是我想更新细节,因为在profile选项卡中,用户应该更新细节,而不是添加新的。您现在可以提出建议吗?这与您现有的问题无关,但通常要做的是将表的主键传递给端点,并使用它来确定是否需要执行插入或更新。如果主键不为null,您知道它应该是一个更新,反之亦然,我的问题既是一个错误,也是一个更新查询,因为我正在进行更新,并将表的正确主键设置为notnull,这意味着主键已经存在于数据库中,因此应该覆盖它,而不是创建新的主键。你能推荐我吗?我是新来的,正在寻求帮助。你能帮我吗?我已经做了更改,现在我写了两篇文章,但当我尝试从那里更新一个或两个字段时,它正在数据库(新用户)中添加数据。我如何更新而不是添加一个新的?你能帮助我吗?更新-替换-cRepo.save(客户);带-cRepo.save(存在)@AjayKumar感谢它的工作,这是我们在保存信息之前需要做的唯一一件额外的事情,以设置值。当然,您将提供/更改一些数据以进行更新。我已经进行了更改,现在我已经写了两个地方的文章,但当我尝试从那里更新一个或两个字段时,它正在数据库中添加数据(新用户)。我如何更新而不是添加一个新的?你能帮助我吗?更新-替换-cRepo.save(客户);带-cRepo.save(存在)@AjayKumar感谢它的工作,我们需要做的唯一额外的事情是在保存信息之前设置值。当然,您将提供/更改一些数据以进行更新。
<form name="myForm" th:action="@{/profile}" th:object="${customer}" **method="post"**>


@RequestMapping(value = "/profile", method = **RequestMethod.PUT**) 
@RequestMapping(value = "/profile" ,method = RequestMethod.POST)
    public ModelAndView updateProfile(@ModelAttribute Customer customer, HttpSession session) {
        
        ModelAndView model = new ModelAndView();
        
        Customer exist = cRepo.findByCustEmail(customer.getCustEmail());

        if(exist != null) {
            if(exist.getCustEmail().equals(session.getAttribute("emailsession"))) {
                
                **exist.setCustId(exist.getCustId());
                exist.setCustName(customer.getCustName());
                exist.setCustEmail(customer.getCustEmail());
                exist.setCustPassword(customer.getCustPassword());**
                
                cRepo.save(exist);
                model.addObject("msg", "User Details has been successfully updated!!");
                
                model.addObject("customer", exist);
                model.setViewName("profile");
            }
                
        }else {
            
            model.addObject("exist", "Please enter correct email address!");
            String email = (String) session.getAttribute("emailsession");
            Customer cust = cRepo.findByCustEmail(email);
            model.addObject("customer", cust);
            model.setViewName("profile");
            
        }
        
        return model;
    }