Home > Enterprise >  In spring boot, GET request is returning 200 but POST request is returning 403
In spring boot, GET request is returning 200 but POST request is returning 403

Time:05-26

I've a spring boot application with springdoc-openapi-ui dependency, and without security. The HTTP GET request is going through (status 200). But the HTTP POST request is not going through (status 403). What could be the reason for the 403 return code on the POST request?

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.chartinvest</groupId>
    <artifactId>ta</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <properties>
        <open-api>1.6.8</open-api>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>${open-api}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
    </dependencies>

</project>

Spring controller class

@RestController
public class TaController {

    @RequestMapping(value = "/ping", method = RequestMethod.GET)
    public @ResponseBody String pingGet() {
        log.info("/ping");
        return "ok";
    }

    @Operation(summary = "test")
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String test(String string) {
        log.info("/test");
        return "ok !!";
    }

swagger

enter image description here

CodePudding user response:

Either you have to use @RequestAttribute within your test method or you have to remove String string argument :

@RequestMapping(value = "/test", method = RequestMethod.POST,@RequestAttribute(value = "string") String string)
    public String test(String string) {
        log.info("/test");
        return "ok !!";
    }

CodePudding user response:

RequestParam annotation is missing. Please use below method. You need to invoke POST method with request param key string

And ResponseBody annotation is also missing.

@Operation(summary = "test")
        @RequestMapping(value = "/test", method = RequestMethod.POST)
        public @ResponseBody String test(@RequestParam("string") String string) {
            log.info("/test");
            return "ok !!";
        }
  • Related