Description
Miroslav Hrúz opened SPR-16190 and commented
In Spring 4 and Spring 5/ Web (Servlet API) you could write annotated controller method without explicitly say @GetMapping
or @PostMapping
and without any @RequestParam
or @ModelAttribute
@RequestMapping("/get/and/post/form-data-www-urleconded")
public String getAndPost(String param1, String param2) {
return "Got: param1="+param1+", param2="+param2;
}
Which does except others:
- Binds /get/and/post/form-data-www-urleconded to be used with HTTP GET and param1, param2 parameters to HTTP query parameters
- Binds /get/and/post/form-data-www-urleconded to be used with HTTP POST form data www-urlencoded and param1, param2 to HTTP POST form data.
- Binds /get/and/post/form-data-www-urleconded to be used with HTTP POST multipart data (FormFieldPart) and param1, param2 to HTTP POST multipart data
In Spring WebFlux 5.0.1 it's not working. It works in Spring Web 5.0.1 and in Spring Web 4.x.
The desired use case is when you want to expose REST API for both HTTP GET and POST and you want to simple write the method ones, not to use @GetMapping
with appropriate @RequestParam
or @PostMapping
with @ModelAttribute
or @RequestBody
. The beauty is not to use any argument annotation at all.
I've attached example projects for spring 4.x, Spring 5.0.1 Web and Spring 5.0.1 WebFlux.
requestMapping_for_post test is failing and should not.
@Test
public void requestMapping_for_get() {
final WebTestClient client = WebTestClient.bindToController(new TestController()).build();
final String responseBody = client.get()
.uri("/get/and/post/form-data-www-urleconded?param1=111¶m2=222")
.exchange()
.expectStatus().isOk()
.expectBody(String.class)
.returnResult().getResponseBody();
Assert.assertEquals("Got: param1=111, param2=222", responseBody);
}
@Test
public void requestMapping_for_post() {
final WebTestClient client = WebTestClient.bindToController(new TestController()).build();
final MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("param1", "111");
formData.add("param2", "222");
final String responseBody = client.post()
.uri("/get/and/post/form-data-www-urleconded")
.body(BodyInserters.fromFormData(formData))
.exchange()
.expectStatus().isOk()
.expectBody(String.class)
.returnResult().getResponseBody();
Assert.assertEquals("Got: param1=111, param2=222", responseBody);
}
Affects: 5.0.1
Attachments:
- FormDataWorkaround.java (2.81 kB)
- spring4-formData.zip (58.26 kB)
- spring5-webflux-formdata.zip (59.16 kB)
- spring5-web-formdata.zip (59.02 kB)
Issue Links:
- Remove support for "request params" from WebFlux [SPR-15508] #20067 Remove support for "request params" from WebFlux