gpt4 book ai didi

spring - RestController 与 Spring WebFlux :Required parameter is not present

转载 作者:行者123 更新时间:2023-12-02 00:34:34 27 4
gpt4 key购买 nike

我用Spring WebFlux写了一个Rest Controller Demo,不能正常运行,源码如下:

@RestController
public class Demo{
@PostMapping(value = "test2")
public Integer getHashCode(@RequestParam("parameters") String parameters){
return parameters.hashCode();
}
}

我用Postman测试了一下,返回:

{
"timestamp": "2018-05-07T07:19:05.303+0000",
"path": "/test2",
"status": 400,
"error": "Bad Request",
"message": "Required String parameter 'parameters' is not present"
}

enter image description here

依赖项:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

I wrote the same controller demo with Spring boot(v2.0.1.RELEASE), it can run correctly. Why can't it run correctly in Spring Webflux?

最佳答案

作为described in the reference documentation ,在请求参数方面,基于 Servlet 的应用程序 (Spring MVC) 和 Spring WebFlux 之间存在细微的行为差异。

在Spring WebFlux中,@RequestParam只会绑定(bind)查询参数。在您的情况下,您的 HTTP 请求未提供此类查询参数,并且您的方法签名未将其标记为可选。

看看您的 Postman 屏幕截图,您似乎打算将 HTTP 表单数据绑定(bind)到该参数,那么您应该看看 command objects相反。

关于spring - RestController 与 Spring WebFlux :Required parameter is not present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50209230/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com