javahtmlspringspring-bootthymeleaf

Method 'POST' is not supported - Spring


I'm trying to send a post request to Spring but I always get this error: Method 'POST' is not supported

enter image description here

I also get this error when trying to post data

enter image description here

I'm using eclipse and spring boot, my project is connected to a postgres database

The connection to my databse is working fine

The program was supposed to get input from a html form and save it in my database.

This is my entity:

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

my repository:

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

}

my controller:

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

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

and my application.properties file:

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

username and password are left out but as I said the connection to the database is working just fine

I tried almost everything and read a lot of posts about similar problems on here but couldn't find a solution.


Solution

  • The only endpoint that accepts POST method in your app is /save because that is the only place where you defined your @PostMapping, so to send POST request you would have to use this url:

    localhost:8080/save

    Also make sure that your request body is valid because you put @Valid annotation.