简体   繁体   English

在 Spring Boot 中,GET 请求返回 200,但 POST 请求返回 403

[英]In spring boot, GET request is returning 200 but POST request is returning 403

I've a spring boot application with springdoc-openapi-ui dependency, and without security.我有一个带有springdoc-openapi-ui依赖项的 Spring Boot 应用程序,并且没有安全性。 The HTTP GET request is going through (status 200). HTTP GET 请求正在通过(状态 200)。 But the HTTP POST request is not going through (status 403).但是 HTTP POST 请求没有通过(状态 403)。 What could be the reason for the 403 return code on the POST request? POST 请求中返回 403 代码的原因可能是什么?

pom.xml 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昂首阔步

在此处输入图像描述

RequestParam annotation is missing.缺少RequestParam注释。 Please use below method.请使用以下方法。 You need to invoke POST method with request param key string您需要使用请求参数键string调用 POST 方法

And ResponseBody annotation is also missing.并且 ResponseBody 注释也丢失了。

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

Either you have to use @RequestParam within your test method or you have to remove String string argument :您必须在test方法中使用@RequestParam或者您必须删除String string参数:

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

this works:这有效:

        @Operation(summary = "test")
        @PostMapping("/test")
        public  String test(@RequestBody String string) {
            log.info("/test");
            return "ok !!";
        }

this will expect the string as a body.这将期望字符串作为一个主体。 please make sure you refresh the swagger interface after you apply the fix.请确保在应用修复后刷新 swagger 界面。

for header:对于标题:

        @Operation(summary = "test")
        @PostMapping("/test")
        public  String test(@RequestHeader("someHeader") String string){
            log.info("/test");
            return "ok !!";
        }

for path variable:对于路径变量:

        @Operation(summary = "test")
        @PostMapping("/test/{somePath}")
        public  String test(@PathVariable("somePath") String string){
            log.info("/test");
            return "ok !!";
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot:MockMvc 为 POST 请求返回空体 - Spring Boot: MockMvc returning empty body for POST request 对端点的 Spring Boot POST 请求返回 Null 值 - Spring Boot POST request to endpoint returning Null values Spring Boot 403禁止在Tomcat 9中使用POST请求 - Spring Boot 403 forbidden with POST request in Tomcat 9 Angular 和 Spring 启动 -> POST 请求响应 403 - Angular and Spring boot -> POST Request response 403 Spring 启动 2.2.4.RELEASE 在 Origin 被定义为 webapp 自己的主机名以外的任何内容时返回 403 获取请求 - Spring boot 2.2.4.RELEASE returning 403 for GET request when Origin is defined as anything other than the webapp's own hostname Spring MVC上的AJAX POST请求返回404 - AJAX POST Request returning 404 on Spring MVC Spring Boot GET 和 POST 返回 Not Found (CrudReposirory) - Spring Boot GET and POST returning Not Found (CrudReposirory) 当使用Keyclaok进行POST请求时,Spring Boot返回403被禁止 - spring boot return 403 forbidden when POST request with Keyclaok Spring 启动 -- 使用 CSRF 令牌发布请求产生 403 错误 - Spring Boot -- Post request with CSRF token produce 403 error Java HTTPS POST 请求返回 200 的响应代码,而我知道它应该返回 302 的响应代码 - Java HTTPS POST Request Is Returning A Response Code Of 200 When I Know It Should Be Returning A Response Code Of 302
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM