Closed
Description
Vitalii Ishchenko opened SPR-17478 and commented
Request validation behaves badly for cases when RequestBody is optional and not provided, but consumes is specified in RequestMapping annotation
i.e. having following method in controller, annotated with PostMapping, request will fail with empty body and no Content-Type. Sending dummy content type will resolve the issue
@PostMapping(value = "/records", consumes = "application/json")
public List<String> queryRecords(@RequestBody(required = false) Map<String, String> filter) {
return Collections.emptyList();
}
Fails:
curl -X "POST" "http://localhost:8080/records"
> POST /records HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 415
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 08 Nov 2018 08:01:09 GMT
<
* Connection #0 to host localhost left intact
{"timestamp":"2018-11-08T08:01:09.927+0000","status":415,"error":"Unsupported Media Type","message":"Content type '' not supported","path":"/records"}
Succeeds:
curl -X "POST" "http://localhost:8080/records" \
-H 'Content-Type: application/json'
> POST /records HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 200
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 08 Nov 2018 08:03:31 GMT
<
* Connection #0 to host localhost left intact
[]
Sample Spring Boot app is attached
Affects: 5.1.2
Attachments:
- demo app.zip (51.97 kB)
Issue Links:
- @RequestBody(required=false) and no Content-Type Header Issue [SPR-17422] #21955
@RequestBody
(required=false) and no Content-Type Header Issue ("duplicates")