java Spring Boot - 不支持请求方法“POST”(不允许方法)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42721597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:49:51  来源:igfitidea点击:

Spring Boot - Request method 'POST' not supported (Method not allowed)

javaspringspring-mvcpost

提问by KnechtRootrecht

As I followed this tutorial: https://spring.io/guides/gs/handling-form-submission/after a while I came to a dead end. I tried to figure out what the problem is for some hours now. I hope you can help me.

当我遵循本教程:https: //spring.io/guides/gs/handling-form-submission/一段时间后,我走到了死胡同。几个小时以来,我一直试图找出问题所在。我希望你能帮助我。

When I submit my form, I get this error-message:

当我提交表单时,我收到此错误消息:

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

My App (App.java):

我的应用程序(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);
  }
}

My Controller (InformationController.java):

我的控制器(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";
  }
}

I created an additional class for security (WebSecurityConf.java):

我创建了一个额外的安全类(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();
  }
}

And I have following two HTML-Files:

我有以下两个 HTML 文件:

infoForm.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:

信息结果.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>

Edit (additional information): my Information.java:

编辑(附加信息):我的 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;
  }

}

my pom-dependencies:

我的 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>

I'm using Java 1.8u121 (jdk).

我正在使用 Java 1.8u121 (jdk)。

Edit2: I tried 3 different versions of spring boot now. Also I downloaded the project from here: https://github.com/spring-guides/gs-handling-form-submissionand added spring boot via pom.xml. The downloaded project does not work for me.

Edit2:我现在尝试了 3 个不同版本的 Spring Boot。我也从这里下载了项目:https: //github.com/spring-guides/gs-handling-form-submission并通过 pom.xml 添加了 spring boot。下载的项目对我不起作用。

I'm getting really frustrated.

我真的很沮丧。

采纳答案by KnechtRootrecht

I found the problem. My pom.xml was wrong. The right one looks like this:

我发现了问题。我的 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>

回答by Sunil Kumar

I copied your exact same classes except the web security class and tried to run. The post method is working for me. The only change I made was returning file names without the .htmlextension.

我复制了除网络安全类之外的完全相同的类并尝试运行。post方法对我有用。我所做的唯一更改是返回没有.html扩展名的文件名。

My controller class looks like this

我的控制器类看起来像这样

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";
    }
}

回答by Prasad Paravatha

Did you try with @RequestParam instead of @ModelAttribute?

您是否尝试过使用 @RequestParam 而不是 @ModelAttribute?

Something like this..

像这样的东西..

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