Spring Boot - 请求方式 'POST' 不支持(不允许的方法)

3
我按照这个教程:https://spring.io/guides/gs/handling-form-submission/,但是在一段时间后我遇到了一个死胡同。我已经试图找出问题几个小时了,希望你能帮助我。

当我提交表单时,我收到了这个错误信息:

 There was an unexpected error (type=Method Not Allowed, status=405).
 Request method 'POST' not supported

我的应用程序(App.java):
package de.poc.logging.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
  public static void main(String[] args) {
    SpringApplication.run(App.class, args);
  }
}

我的控制器(InformationController.java):
package de.poc.logging.main;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/info")
public class InformationController {
  @RequestMapping(method = RequestMethod.GET, produces = "text/html")
  public String infoForm(Model model) {
    model.addAttribute("information", new Information());
    return "infoForm.html";
  }

  @RequestMapping(method = RequestMethod.POST, produces = "text/html")
  public String infoSubmit(@ModelAttribute Information information) {
    return "infoResult.html";
  }
}

我创建了一个额外的安全类(WebSecurityConf.java):
package de.poc.logging.main.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.headers()
        .frameOptions().sameOrigin()
        .httpStrictTransportSecurity().disable();
    http.csrf().disable();
  }
}

我有以下两个HTML文件:
infoForm.html:
    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Getting Started: Handling Form Submission</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    </head>
    <body>
    <h1>Form</h1>
    <form action="#" th:action="@{/info}" th:object="${information}" method="post">
        <p>Id: <input type="text" th:field="*{id}"/></p>
        <p>Message: <input type="text" th:field="*{content}"/></p>
        <p><input type="submit" value="Submit"/>
            <input type="reset" value="Reset"/></p>
        <!--<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>-->
    </form>
    </body>
    </html>

infoResult.html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + ${information.id}"/>
<p th:text="'content: ' + ${information.content}"/>
<a href="/info">Submit another message</a>
</body>
</html>

编辑(附加信息): 我的 Information.java 文件:

package de.poc.logging.main;

public class Information {
  private long id;
  private String content;

  public long getId() {
    return id;
  }

  public String getContent() {
    return content;
  }

  public void setId(long id) {
    this.id = id;
  }

  public void setContent(String content) {
    this.content = content;
  }

}

我的POM依赖:
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <version>1.5.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>
    </dependencies>

我正在使用Java 1.8u121 (jdk)。
编辑2: 我现在尝试了3个不同版本的Spring Boot。 我还从这里下载了项目:https://github.com/spring-guides/gs-handling-form-submission,并通过pom.xml添加了Spring Boot。 但是下载的项目对我来说无法工作。
我感到非常沮丧。

如果删除安全配置,它能正常工作吗? - Sasha Shpota
不,我担心那并没有改变任何事情。 - KnechtRootrecht
3个回答

2

我复制了你的所有类,除了web安全类之外,并尝试运行。对我而言,post方法是有效的。我所做的唯一更改是返回文件名时不包含.html扩展名。

我的控制器类如下:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.models.Information;

@Controller
@RequestMapping(value = "/info")
public class InformationController {

    @RequestMapping(method = RequestMethod.GET, produces = "text/html")
    public String infoForm(Model model) {

        model.addAttribute("information", new Information());
        return "infoForm";
    }

    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String infoSubmit(@ModelAttribute Information information) {
        return "infoResult";
    }
}

如果我删除 .html 扩展名,表单甚至无法被调用。我还没有弄清楚原因。(出现了意外错误 (类型=未找到,状态=404)。没有可用的消息) - 我已经在我的问题中添加了一些额外的信息。 - KnechtRootrecht

1
我找到了问题所在。我的pom.xml文件有误。 正确的文件应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-handling-form-submission</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>


你在pom.xml文件中做了哪些更改才使它工作了? - FoggyDay
我必须将Spring Boot Starter添加为该项目的父级。 - KnechtRootrecht

0
你试过使用@RequestParam而不是@ModelAttribute吗?
就像这样...
@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String infoSubmit(@RequestParam("info") Information information) {
        return "infoResult.html";
}

谢谢您的建议。我尝试了,但是出现了一个错误:“所需的信息参数'information'不存在”。 - KnechtRootrecht

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接