spring-boottomcathttp-status-code-302

Tomcat redirects all post request to get


I have issue with tomcat 9.0.50. I have a basic Springboot application to deploy and all my post requests are actually redirected to GET request.

here is my Main class:

@SpringBootApplication
public class WsApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(WsApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WsApplication.class);
    }
}

Here i simply extend my class with SpringBootServletInitializer and I added the configure method.

Second point: I created a GET and a POST request in a controller.

@RestController
@RequestMapping("/")
public class AppController {
    @GetMapping
    public String hello() {
        return "Hello";
    }

    @PostMapping
    public String helloName(@RequestBody String name) {
        return "Hello " + name;
    }
}

I also have my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>

    <artifactId>ws-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ws-facturx</name>
    <description>ws-facturx</description>
    <packaging>war</packaging>


    <properties>
        <java.version>11</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

In my application.prpperties I configured SQL and application name:

# ws-application
spring.application.name=ws-application

# postgresql
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL81Dialect

It should normally work correctly. I can send GET request to my base URL and do retrieve the expected response. When I send a POST request, i obtain a 302 response. I created a dummy application to check if issue was coming from the application.

I tried the call using postman and with a curl request:

curl -i -X POST -H "Content-Type: text/plain" -d "John" http://localhost:8080/ws-application

Is there any issue with tomcat or Springboot ? The java version used is provided in jdk-11.0.6


Solution

  • I don't really know "why" I have been able to solve this issue but here is the "how".

    @Slf4j
    @RestController
    @RequestMapping("/api")
    public class FacturxController {
        @GetMapping
        public String hello() {
            return "Hello";
        }
    
        @PostMapping
        public String helloName(@RequestBody String name) {
            log.info("Hello Name");
            return "Hello " + name;
        }
    }
    

    this app has just one controller, but it seems that it required to set a path to the RequestMapping.