不支持“POST”方法 - Spring

问题描述 投票:0回答:1

我正在尝试向 Spring 发送发布请求,但总是收到此错误:不支持方法“POST”

enter image description here

我在尝试发布数据时也遇到此错误

enter image description here

我正在使用 eclipse 和 spring boot,我的项目连接到 postgres 数据库

与我的数据库的连接工作正常

该程序应该从 html 表单获取输入并将其保存在我的数据库中。

这是我的实体:

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

我的存储库:

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> {

}

我的控制器:

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

用户名和密码被省略,但正如我所说,与数据库的连接工作正常

我几乎尝试了所有方法,并在这里阅读了很多有关类似问题的帖子,但找不到解决方案。

java html spring spring-boot thymeleaf
1个回答
0
投票

应用程序中接受 POST 方法的唯一端点是 /save,因为这是您定义 @PostMapping 的唯一位置,因此要发送 POST 请求,您必须使用此 url:localhost:8080/post,还要确保您的请求正文是有效的,因为您添加了 @Valid 注释。

© www.soinside.com 2019 - 2024. All rights reserved.