不支持请求方法 'POST' - Java Spring 引导

Request method 'POST' not supported - Java Spring Boot

我的注册页面工作正常,当点击“注册”时,它接受输入并推送到数据库。但是,当我单击转到“列出项目”页面时,当单击“列表项目”时,该页面同样将输入推送到数据库中的 table,它会出现“请求方法 'POST' 不支持”。因此,从主登录菜单页面单击时,不会出现“列出项目”页面。

部分控制器class此处:

@GetMapping("/register")
public String goToRegisterPage(Model model) {
    User user = new User();
    model.addAttribute("user", user);
    return "register_page";
}

@GetMapping("/go_to_create_item_page")
public String goToCreateItemPage(Model model) {
    FreeItem freeItem = new FreeItem();
    model.addAttribute("freeItem", freeItem);
    return "create_item_page";
}

html 文件的一部分,提供转到 create_item_page:

 <div>
            <form th:action="@{/update_user_page}" method="post">
                <input type="submit" value="Update account" />
            </form>
        </div>
        <div>
            <form th:action="@{/go_to_create_item_page}" method="post">
                <input type="submit" value="List an Item" />
            </form>
        </div>
    
   

create_item_page:

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
    
        <meta charset="ISO-8859-1">
        <title>Create Item</title>
        <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css" />
    
    </head>
    <body>
    
        <div class="container text-center">
    
        <div>
            <h1>Create an Item to list publicly</h1>
        </div>
    
        <form th:action="@{/process_created_item}" method="post"
            style="max-width: 600px; margin: 0 auto;" 
            th:object="${freeItem}">
    
                <div class="m-3">
    
                    <div class="form-group row">
                        <label class="col-form-label col-4">Item Name</label>
                        <div class="col-8">
                            <input type="text" class="form-control" th:field="*{itemName}" required 
                            minlength="2" maxlength="20" />
                        </div>
    
                    </div>
    
                    <div class="form-group row">
                        <label class="col-form-label col-4">Item Description</label>
                        <div class="col-8">
                            <input type="text" class="form-control"
                                th:field="*{itemDescription}" required 
                                minlength="6" maxlength="10" />
                        </div>
                    </div>
    
                    <div class="form-group row">
                        <label class="col-form-label col-4">Quantity Of Item</label>
                        <div class="col-8">
                            <input type="text" class="form-control" th:field="*{quantityOfItem}" required 
                                minlength="2" maxlength="20" />
                        </div>
    
                    </div>
    
                    <div class="form-group row">
                        <label class="col-form-label col-4">General Item Location</label>
                        <div class="col-8">
                            <input type="text" class="form-control" th:field="*{itemLocation}"
                                required minlength="2" maxlength="20" />
                        </div>
                    </div>
    
                    <div class="form-group row">
                        <label class="col-form-label col-4">E-mail for contact</label>
                        <div class="col-8">
                            <input type="email" class="form-control" th:field="*{email}"
                                required />
                        </div>
                    </div>
    
                    <div>
                        <button type="submit" class="btn btn-primary">List Item</button>
                    </div>
    
    
    
                </div>
    
            </form>
    
        </div>
    
    </body>
    </html>

以及指定要推送到数据库的对象的 FreeItem class。

 package com.marinelli.model;

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.Table;

 @Entity
 @Table(name= "free_items")
 public class FreeItem {
    
    public FreeItem(String itemName, String itemDescription, String quantityOfItem, String itemLocation, String email) {
        super();
        this.itemName = itemName;
        this.itemDescription = itemDescription;
        this.quantityOfItem = quantityOfItem;
        this.itemLocation = itemLocation;
        this.email = email;
    }

    public FreeItem() {
        
    }

    @Id
    @Column(nullable = false, unique = true, length = 64)
    private String itemName;
    
    @Column(nullable = false, length = 64)
    private String itemDescription;
    
    @Column(nullable = false, length = 25)
    private String quantityOfItem;
    
    @Column(nullable = false, length = 25)
    private String itemLocation;
    
    @Column(nullable = false, unique = true, length = 45)
    private String email;

    
    
    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemDescription() {
        return itemDescription;
    }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }

    public String getQuantityOfItem() {
        return quantityOfItem;
    }

    public void setQuantityOfItem(String quantityOfItem) {
        this.quantityOfItem = quantityOfItem;
    }

    public String getItemLocation() {
        return itemLocation;
    }

    public void setItemLocation(String itemLocation) {
        this.itemLocation = itemLocation;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "FreeItem [itemName=" + itemName + ", itemDescription=" + itemDescription + ", quantityOfItem="
                + quantityOfItem + ", itemLocation=" + itemLocation + ", email=" + email + "]";
    }

    
}

该错误清楚地表明您对所提供的端点使用的方法不正确。

在您的例子中,/go_to_create_item_page 定义为 GET,您的表单声明方法 = post。将它们更改为同时使用 POST 或 GET 应该可以解决问题(就 API 设计而言,它应该是 GET,因为您是从服务器获取资源而不是创建/更新)。

您需要创建 go_to_create_item_pageupdate_user_page 作为 @Postmapping("....")

@Postmapping("/update_user_page")
public String goToUpdateUserPage(Model model) {
...
...
...
}

@Postmapping("/go_to_create_item_page")
public String goToCreateItemPage(Model model) {
...
...
...
}