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