diff --git a/spring-cloud-gateway-mvc/src/main/java/org/springframework/cloud/gateway/mvc/ProxyExchange.java b/spring-cloud-gateway-mvc/src/main/java/org/springframework/cloud/gateway/mvc/ProxyExchange.java index b7efe59d..e0e34e1e 100644 --- a/spring-cloud-gateway-mvc/src/main/java/org/springframework/cloud/gateway/mvc/ProxyExchange.java +++ b/spring-cloud-gateway-mvc/src/main/java/org/springframework/cloud/gateway/mvc/ProxyExchange.java @@ -462,7 +462,7 @@ public class ProxyExchange { protected static class BodyGrabber { - public Object body(@RequestBody Object body) { + public Object body(@RequestBody(required = false) Object body) { return body; } diff --git a/spring-cloud-gateway-mvc/src/test/java/org/springframework/cloud/gateway/mvc/ProductionConfigurationTests.java b/spring-cloud-gateway-mvc/src/test/java/org/springframework/cloud/gateway/mvc/ProductionConfigurationTests.java index bf9e8677..93c49651 100644 --- a/spring-cloud-gateway-mvc/src/test/java/org/springframework/cloud/gateway/mvc/ProductionConfigurationTests.java +++ b/spring-cloud-gateway-mvc/src/test/java/org/springframework/cloud/gateway/mvc/ProductionConfigurationTests.java @@ -235,6 +235,12 @@ public class ProductionConfigurationTests { .isEqualTo("host=localhost;foobar"); } + @Test + public void noBody() throws Exception { + Foo foo = rest.postForObject("/proxy/no-body", null, Foo.class); + assertThat(foo.getName()).isEqualTo("hello"); + } + @Test @SuppressWarnings({ "Duplicates", "unchecked" }) public void headers() throws Exception { @@ -351,6 +357,12 @@ public class ProductionConfigurationTests { .body(response.getBody().iterator().next())); } + @PostMapping("/proxy/no-body") + public ResponseEntity noBody(ProxyExchange proxy) throws Exception { + return proxy.uri(home.toString() + "/foos") + .post(); + } + @GetMapping("/forward/**") public void forward(ProxyExchange proxy) throws Exception { String path = proxy.path("/forward"); @@ -413,6 +425,11 @@ public class ProductionConfigurationTests { return Arrays.asList(new Foo("hello")); } + @PostMapping("/foos") + public Foo postFoos() { + return new Foo("hello"); + } + @GetMapping("/foos/{id}") public Foo foo(@PathVariable Integer id, @RequestHeader HttpHeaders headers) { String custom = headers.getFirst("X-Custom");