Issue
I wrote a Rest Controller Demo with Spring WebFlux,it can't run correctly, source code as follows:
@RestController
public class Demo{
@PostMapping(value = "test2")
public Integer getHashCode(@RequestParam("parameters") String parameters){
return parameters.hashCode();
}
}
I used Postman to test it, the returning:
{
"timestamp": "2018-05-07T07:19:05.303+0000",
"path": "/test2",
"status": 400,
"error": "Bad Request",
"message": "Required String parameter 'parameters' is not present"
}
dependencies:
<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?
Solution
As described in the reference documentation, there is a slight behavior difference between Servlet-based apps (Spring MVC) and Spring WebFlux with regards to request parameters.
In Spring WebFlux, @RequestParam
will only bind query parameters. In your case, your HTTP request is not providing such a query parameter and your method signature does not mark it as optional.
Looking at your Postman screenshot, it looks like you meant to bind HTTP form data to that argument, then you should probably take a look at command objects instead.
Answered By - Brian Clozel