[英]In spring boot, GET request is returning 200 but POST request is returning 403
我有一个带有springdoc-openapi-ui依赖项的 Spring Boot 应用程序,并且没有安全性。 HTTP GET 请求正在通过(状态 200)。 但是 HTTP POST 请求没有通过(状态 403)。 POST 请求中返回 403 代码的原因可能是什么?
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>
弹簧控制器类
@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 !!";
}
昂首阔步
缺少RequestParam注释。 请使用以下方法。 您需要使用请求参数键string
调用 POST 方法
并且 ResponseBody 注释也丢失了。
@Operation(summary = "test")
@RequestMapping(value = "/test", method = RequestMethod.POST)
public @ResponseBody String test(@RequestParam("string") String string) {
log.info("/test");
return "ok !!";
}
您必须在test
方法中使用@RequestParam
或者您必须删除String string
参数:
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(@RequestParam(name= "string")String string) {
log.info("/test");
return "ok !!";
}
这有效:
@Operation(summary = "test")
@PostMapping("/test")
public String test(@RequestBody String string) {
log.info("/test");
return "ok !!";
}
这将期望字符串作为一个主体。 请确保在应用修复后刷新 swagger 界面。
对于标题:
@Operation(summary = "test")
@PostMapping("/test")
public String test(@RequestHeader("someHeader") String string){
log.info("/test");
return "ok !!";
}
对于路径变量:
@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.