gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:51:17 25 4
gpt4 key购买 nike

当我遵循本教程时: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);
}
}

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

编辑(附加信息):我的信息.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。下载的项目对我不起作用。

我真的很沮丧。

最佳答案

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

我的 Controller 类如下所示

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

关于java - Spring Boot - 不支持请求方法 'POST'(方法不允许),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42721597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com