Spring Boot POST请求不支持错误排查求助
Spring Boot POST请求报错:Method 'POST' is not supported
发送POST请求时始终收到错误提示:Method 'POST' is not supported,提交表单数据时同样出现该错误。
环境与预期功能
- 使用Eclipse + Spring Boot
- 已连接PostgreSQL数据库,连接正常
- 预期功能:从HTML表单获取输入并保存到数据库
实体类(xRomy.java)
package entity; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; @Entity @Table(name = "xromy") public class xRomy { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) // muss wohl public sein public Integer id; private String name; private Integer alter; public xRomy() { } public xRomy(String name, Integer alter) { this.name = name; this.alter = alter; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAlter() { return alter; } public void setAlter(Integer alter) { this.alter = alter; } @Override public String toString() { return "xRomy{" + "\n id=" + id + "\n name=" + name + "\n alter=" + alter + "}"; } }
Repository(RomyRep.java)
package repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import entity.xRomy; @Repository public interface RomyRep extends JpaRepository<xRomy, Integer> { }
Controller(ConttrollNeuDs.java)
package controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import entity.xRomy; import jakarta.validation.Valid; import repository.RomyRep; @RestController public class ConttrollNeuDs { @Autowired RomyRep romyRep; // um neuen Eintrag eizugeben @GetMapping("/neuerEintrag") public ModelAndView getName() { ModelAndView model = new ModelAndView("newName"); model.addObject("xromy", new xRomy()); return model; } // sollte neuen Eintrag speichern @PostMapping("/save") public String postNew(@Valid xRomy xromy, BindingResult result, ModelAndView model) { model.addObject("xromy", xromy); xRomy Xromy = new xRomy(); Xromy.setName(xromy.getName()); Xromy.setAlter(xromy.getAlter()); romyRep.save(Xromy); return "redirect:/testView"; } }
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>hilfe</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hilfe</name> <description>Demo project for Spring Boot</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
spring.application.name=hilfe #Datenbank Vb spring.datasource.url=jdbc:postgresql://localhost:5433/romy spring.datasource.username= spring.datasource.password= spring.datasource.driver-class-name=org.postgresql.Driver # spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect # spring.mvc.static-path-pattern=/resources/** #test spring.jpa.generate-ddl=true
补充说明
数据库用户名和密码已省略,数据库连接正常。尝试过各种方法及查阅相关帖子,仍未解决问题。
内容的提问来源于stack exchange,提问作者adi
相关产品推荐
相关产品推荐

