[英]Spring Boot Request method 'POST' not supported
簡單 Spring 引導 POST 請求不起作用並返回:“不支持請求方法‘POST’”錯誤消息。
代碼本身對我來說看起來不錯,但我不太確定 pom.xml。 如果問題出在 pom.xml 上,那么這種情況就沒有希望了,因為我什至不知道所有這些依賴項的作用,也不知道為什么會有重復項。
這是我的 controller class:
@Controller
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/")
public String list(Model model) {
model.addAttribute("students", studentRepository.findAll());
return "index";
}
@PostMapping("/")
public String add(@RequestParam String name) {
Student student = new Student(name);
this.studentRepository.save(student);
return "redirect:/";
}
}
Index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Test</title>
</head>
<body>
<p>List all students </p>
<p>Add a Student</p>
<form th:action="@{/}" method="POST">
<input type="text" name="name"/>
<input type="submit"/>
</form>
<p>Students</p>
<p th:each="student : ${students}">
<span th:text="${student.name}">student name</span>
</p>
</body>
</html>
密碼.xml
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.fluentlenium</groupId>
<artifactId>fluentlenium-junit</artifactId>
<version>3.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fluentlenium</groupId>
<artifactId>fluentlenium-assertj</artifactId>
<version>3.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
該代碼應該將學生添加到 studentRepository 並重定向回 index.html 並立即顯示添加的學生。
來自 soapUI 的錯誤消息:
"timestamp": "2019-05-24T09:02:36.088+0000",
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'POST' not supported",
"trace": "org.springframework.web.HttpRequestMethodNotSupportedException:
"path": "/"
完整跟蹤:
> "trace":
> "org.springframework.web.HttpRequestMethodNotSupportedException:
> Request method 'POST' not supported\r\n\tat
> org.springframework.web.servlet.support.WebContentGenerator.checkRequest(WebContentGenerator.java:380)\r\n\tat
> org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:163)\r\n\tat
> org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52)\r\n\tat
> org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)\r\n\tat
> org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)\r\n\tat
> org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)\r\n\tat
> org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)\r\n\tat
> javax.servlet.http.HttpServlet.service(HttpServlet.java:660)\r\n\tat
> org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)\r\n\tat
> javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)\r\n\tat
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)\r\n\tat
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)\r\n\tat
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)\r\n\tat
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)\r\n\tat
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\r\n\tat
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)\r\n\tat
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\r\n\tat
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\r\n\tat
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\r\n\tat
> org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)\r\n\tat
> org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\r\n\tat
> org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)\r\n\tat
> org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747)\r\n\tat
> org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)\r\n\tat
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)\r\n\tat
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat
> java.lang.Thread.run(Thread.java:745)\r\n"
應用程序.properties
server.port=8083
spring.profiles.active=@spring.profiles.active@
spring.datasource.url=jdbc:h2:file:./database;create=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
工作正常的教程:
Controller class
@Controller
public class ItemDatabaseController {
@Autowired
private ItemRepository itemRepository;
@GetMapping("/")
public String list(Model model) {
model.addAttribute("items", this.itemRepository.findAll());
return "index";
}
@PostMapping("/")
public String add(@RequestParam String name) {
this.itemRepository.save(new Item(name));
return "redirect:/";
}
}
index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Item Database</title>
</head>
<body>
<h1>Item Database</h1>
<table>
<tr>
<th>Items</th>
</tr>
<tr th:each="item : ${items}">
<td th:text="${item.name}">Item name</td>
</tr>
</table>
<h2>Add an item</h2>
<form th:action="@{/}" method="POST">
<span>Name:</span>
<input type="text" name="name"/>
<input type="submit" value="Add!"/>
</form>
</body>
</html>
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.fluentlenium</groupId>
<artifactId>fluentlenium-junit</artifactId>
<version>3.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fluentlenium</groupId>
<artifactId>fluentlenium-assertj</artifactId>
<version>3.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
這兩個幾乎是一樣的,只是下載了本教程並且我自己的項目是在 Eclipse 中創建的,作為 Spring Boot: Spring Starter Project。 這是一個 Eclipse 插件。
使用@RestController代替@Controller Annotation或
只需使用
@PostMapping("/")
@ResponseBody
我建議對現有代碼進行一些調整。 請查看是否有效。
@Controller
@RequestMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/")
public String list(Model model) {
model.addAttribute("students", studentRepository.findAll());
return "index";
}
@PostMapping(path = "/", produces = MediaType.APPLICATION_XML_VALUE)
public String add(@RequestBody String name) {
Student student = new Student(name);
this.studentRepository.save(student);
return "redirect:/";
}
}
希望它能解決問題。
我認為,如果您正在從表單執行請求,則@RequestParam
注釋可能是錯誤的。 這用於從url( ?name=myname
)獲取參數。 您可能要嘗試@RequestBody
。
我們可以看看您的resources
文件夾的文件路徑嗎? 當我的 html 位於static
文件夾中時,我得到了這個Request method 'POST' not supported
錯誤,當我將 html 移動到templates
時錯誤消失了。 我認為基本上 Thymeleaf 不會操作 static 文件夾中的static
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.