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

Request method 'GET' not supported Error after change @GetMapping to Delete method

我的网络应用程序有问题。我是一名学生,还在学习。这是一个简单的论坛,登录、注册、添加新主题和帖子。也可以删除没有问题的主题 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";
    }
}

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>