不支持请求方法“GET”将@GetMapping 更改为Delete 方法后出错

Posted

技术标签:

【中文标题】不支持请求方法“GET”将@GetMapping 更改为Delete 方法后出错【英文标题】:Request method 'GET' not supported Error after change @GetMapping to Delete method 【发布时间】:2017-07-17 09:32:50 【问题描述】:

我的网络应用程序有问题。我是学生,还在学习。这是一个简单的论坛,包括登录、注册、添加新主题和帖子。也可以删除没有问题的主题 GetMapping 方法。不幸的是,我得到了一个将 GetMapping 更改为 Delete 的命令,并且在更改后的内容应用程序中出现错误: “出现意外错误(类型 = 不允许的方法,状态 = 405)。请求方法 'GET' 不支持' 我在网上寻找解决这个问题的方法,但是在检查了各种选项后,它仍然是同样的错误。我在这个主题上也没有足够的经验,因此对我来说很多说明都不清楚。所以请帮忙。

所以这是删除更改之前的 topic.html 视图:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Forum - Topic</title>
</head>
<body background="http://2.bp.blogspot.com/-BsL9gRE80Ug/U0OgeWbbxtI/AAAAAAAAF-w/teXrzw-TBcU/s1600/nacre-background-tile.jpg">
<table class="table table-striped">
<a href="http://localhost:8080/forum">Powrot</a>
  <tr>
   <th>Title: </th>
	<th><p th:text="$topicName" /></th>
  </tr>
  <tr>
	<td th:text="$topicAuthor" ></td>
	<td th:text="$topicDate" ></td>
	<table border="1">
	<td th:text="$topicContent"></td>
	</table>
  </tr>
  <table>
	  	<br><b>-------------------------------------------------------</b></br>
	  	<a th:href="@/new_message(id=$topicId)">Add new post</a>
	  	<br><a th:href="@/delete(id=$topicId)">Delete this topic</a></br>
	  	<br><b>-------------------------------------------------------</b></br>
  </table>
</table>
<table class="table table-striped">
  <tr th:each="message,iterStat : $list">
    <td th:text="$message.author"></td>
    <td th:text="$message.date"></td>
    <table border="1">
    	<td th:text="$message.content"></td>
    </table>
    <table>
    	<p> - - - - - - - - </p>
    </table>
  </tr>
</table>
</body>
</html>

修改后:

...
    <table>
        <br><b>-------------------------------------------------------</b></br>
        <a th:href="@/new_message(id=$topicId)">Add new post</a>
        <form action="#" th:action="@/delete(id=$topicId)" method="delete">
      <br><a th:href="@/delete(id=$topicId)">Delete this topic</a></br>
  </form>
        <br><b>-------------------------------------------------------</b></br>
    </table>
...

我还编辑了控制器。这是使用 getMapping 的先前工作版本:

@Controller public class TopicController

@Autowired
private TopicRepository topicRepository;

@Autowired
private MessageRepository messageRepository;

@Autowired
private UserRepository userRepository;

@GetMapping("/delete")
public String deleteTopic(@CookieValue(value = "userId", defaultValue = "-1") String userId, 
        @RequestParam("id") String topicId, Model model)

    if(userId.equals("-1"))
    
        model.addAttribute("user", new User());
        return "login";           
    
    else
    
        Topic topic = topicRepository.findByIdIn(Integer.parseInt(topicId));
        if(topic.getUserId() == Integer.parseInt(userId)) 
        
            topicRepository.delete(topic);
        
        return "redirect:/forum";
    


还有新版本,那不行:

@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
public @ResponseBody String deleteTopic(@CookieValue(value = "userId", defaultValue = "-1") String userId, 
        @RequestParam("id") String topicId, @ModelAttribute Topic topic, Model model)

    if(userId.equals("-1"))
    
        model.addAttribute("user", new User());
        return "login";           
    
    else
    
        Topic topicDB = topicRepository.findByIdIn(Integer.parseInt(topicId));
        if(topicDB.getUserId() == Integer.parseInt(userId)) 
        
            topicRepository.delete(topicDB);
        
        return "redirect:/forum";
    

【问题讨论】:

【参考方案1】:

HTML 表单不支持DELETE 方法。因此,当您编写以下内容时,您的浏览器只会使用普通的GET

<form action="#" th:action="@/delete(id=$topicId)" method="delete">

使用POST 方法,因为您更改了服务器上的数据。如果您想让应用程序认为使用了DELETE 方法,请使用带有隐藏字段的HiddenHttpMethodFilter:

<form action="#" th:action="@/delete(id=$topicId)" method="post">
    <input type="hidden" name="_method" value="delete">
    <br><a th:href="@/delete(id=$topicId)">Delete this topic</a></br>
</form>

如果您使用thymeleaf-spring >= 2.0.3,您可以使用th:method 属性,如果需要,thymeleaf 会自动创建隐藏字段。

<form action="#" th:action="@/delete(id=$topicId)" th:method="delete">
    <br><a th:href="@/delete(id=$topicId)">Delete this topic</a></br>
</form>

【讨论】:

嗯,也许我做错了什么,但这些方法都没有帮助:( 您是否正确注册了 HiddenHttpMethodFilter?有关详细信息,请查看文档 (docs.spring.io/spring-boot/docs/current/reference/html/…)。此答案 (***.com/a/26157610/2847174) 包含另一个过滤器的示例。 所以,我正在和我的老师交谈,他告诉我,我有两个选择。 1.制作javascript,然后使用delete,或者2.制作新的Controller,只需删除,然后使用Postman检查控制器是否正常工作。我选择了第二个选项并且能够完成这项工作。我对该主题的评价很高,因此您可以将其视为解决方案的一部分。

以上是关于不支持请求方法“GET”将@GetMapping 更改为Delete 方法后出错的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot之get请求404

@RequestMapping@GetMapping@PostMapping区别详解——Web网络系列学习笔记

@RequestMapping@GetMapping@PostMapping区别详解——Web网络系列学习笔记

405 在客户端请求命中 Spring MVC 中的拦截器之前不支持请求方法“GET”

为什么@GetMapping方法在发送响应时返回请求参数?

post请求用啥mapping