diff --git a/SPR-13490/src/main/java/org/springframework/issues/TestController.java b/SPR-13490/src/main/java/org/springframework/issues/TestController.java index 7ca6206e..460140a5 100644 --- a/SPR-13490/src/main/java/org/springframework/issues/TestController.java +++ b/SPR-13490/src/main/java/org/springframework/issues/TestController.java @@ -15,8 +15,13 @@ public void initBinder(WebDataBinder binder) { binder.addValidators(new PayloadValidator()); } - @RequestMapping("/") - public String emptyBody(@RequestBody @Validated Payload payload) { + @RequestMapping("/mandatory-body") + public String mandatoryBody(@RequestBody @Validated Payload payload) { + return "hello"; + } + + @RequestMapping("/optional-body") + public String optionalBody(@RequestBody(required = false) @Validated Payload payload) { return "hello"; } } diff --git a/SPR-13490/src/test/java/org/springframework/issues/TestControllerTest.java b/SPR-13490/src/test/java/org/springframework/issues/TestControllerTest.java index 27ffb513..6b98d658 100644 --- a/SPR-13490/src/test/java/org/springframework/issues/TestControllerTest.java +++ b/SPR-13490/src/test/java/org/springframework/issues/TestControllerTest.java @@ -24,19 +24,31 @@ public void prepare() { @Test public void shouldSend200WithValidPayload() throws Exception { - mockMvc.perform(post("/").content("{\"message\":\"spring\"}").contentType(MediaType.APPLICATION_JSON)) + mockMvc.perform(post("/mandatory-body").content("{\"message\":\"spring\"}").contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string("\"hello\"")); } @Test public void shouldSend400WithEmptyBody() throws Exception { - mockMvc.perform(post("/")).andExpect(status().isBadRequest()); + mockMvc.perform(post("/mandatory-body")).andExpect(status().isBadRequest()); } @Test public void shouldSend400WithEmptyJsonObject() throws Exception { - mockMvc.perform(post("/").content("{}").contentType(MediaType.APPLICATION_JSON)) + mockMvc.perform(post("/mandatory-body").content("{}").contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()); } + + @Test + public void shouldSend400WithNullJsonObject() throws Exception { + mockMvc.perform(post("/mandatory-body").content("null").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()); + } + + @Test + public void shouldSend200WithNullJsonObject() throws Exception { + mockMvc.perform(post("/optional-body").content("null").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } } \ No newline at end of file