[英]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.