Spring说请求方法' DELETE'尝试删除数据库项时不支持

时间:2017-11-13 20:05:57

标签: java spring jpa

我在一个学校项目上工作,在那里你将Todo-items添加到数据库,显示它们,并授予用户访问为每个项目创建的各个页面的权限。我使用Spring和我的删除按钮,或者显示特定任务的单个页面都不起作用。在这个项目中,我们使用JPA来处理数据库。

继承我的TodoItemDatabaseController:

package wad.tododatabase;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TodoItemDatabaseController {

    @Autowired
    private TodoRepository todoRepository;

    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute
        ("items", this.todoRepository.findAll());
        return "index";
    }

这是删除无效。

    @DeleteMapping("/{itemId}")
    public String remove(Model model, @PathVariable Long itemId) {
        this.todoRepository.delete(this.todoRepository.getOne(itemId));
        return "redirect:/";
    }


    @PostMapping("/")
    public String post(@RequestParam String name) {
        if (!name.trim().isEmpty()) {
            TodoItem item = new TodoItem(name);
            todoRepository.save(item);
        }

        return "redirect:/";
    }

我在这里尝试获取特定ID,为项目创建单独的页面。

    @GetMapping("/{id}")
    public String findOne(Model model, @PathVariable Long id) {
        TodoItem t1 = todoRepository.getOne(id);
        t1.count();
        model.addAttribute("item", t1);
        return "item"; 
    }


}

这是我的html文件。我发布了index.html文件的DELETE部分和item.html,它是单个待办事项的页面。

的index.html:

<table>
            <tr>
                <th>Task name</th>
                <th></th>
            </tr>

            <tr th:each="item : ${items}">
                <td><a th:href="@{/{item}(item=${item.id})}" th:text="${item.name}">Item name</a></td>
                <td><form th:action="@{/{item}(item=${item.id})}" th:method="DELETE"><input type="submit" value="Done!"/></form></td>
            </tr>
        </table>

Item.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>TodoDatabase</title>
    </head>

    <body>
        <h1 th:text="${item.name}">item name</h1>

        <p>Item has been checked a total of <span th:text="${item.checked}">-1</span> times.</p>

        <a th:href="@{/}">Back to listing.</a>
    </body>
</html>

Spring还提供了2个WARN的声明,我猜他们是警告:

2017-11-13 21:52:13.902  WARN 29312 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound             : Request method 'DELETE' not supported
2017-11-13 21:52:13.903  WARN 29312 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported

这是我的TodoRepository界面:

package wad.tododatabase;

/**
 *
 * @author riku
 * 
 */
import org.springframework.data.jpa.repository.JpaRepository;

public interface TodoRepository extends JpaRepository<TodoItem, Long> {


}

任何建议如何让这项工作?

1 个答案:

答案 0 :(得分:0)

尝试使用此注释:

@RequestMapping(value = "/{itemId}"", method = RequestMethod.DELETE)
    public @ResponseBody String remove(Model model, @PathVariable Long itemId) {
        this.todoRepository.delete(this.todoRepository.getOne(itemId));
        return "redirect:/";
    }