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

时间:2017-03-10 15:08:15

标签: java spring spring-mvc post

当我按照本教程: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:

    <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)。

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

我真的很沮丧。

3 个答案:

答案 0 :(得分:0)

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

像这样......

result = inside(1.00001, 1.00001, 1, 1, 2, 2);
int expected = tbd_code();
char input1[119] = ("1.00001, 1.00001, 1, 1, 2, 2");

// No ; after the )
if (result != expected) {
    printf("%s test:%s result:d expected:d\n", errormessage, input1, result, expected);
    totalErrors++; 
}

答案 1 :(得分:0)

我复制了除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";
    }
}

答案 2 :(得分:0)

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