From my understanding when Request Method is Get we should not have Request body, we use @RequestParam
to read the url parameters. But spring test seems to be allowing it. Below is a sample code.
@Controller
public class SomeController {
@RequestMapping(value = "/abc", method = RequestMethod.GET)
public String greeting(@RequestBody Student student) {
return "Hello "+ student.name;
}
}
This is the test code and jsonString
is the string representation of a Student
object.
@Test
public void shouldReturnMessage() throws Exception {
this.mockMvc.perform(get("/abc").content(jsonString)).andDo(print()).andExpect(status().isOk());
}
My question here is, should Spring stop allowing @RequestBody
with Get
request? The above test runs fine and it's able to return me the student name. But when I try to call the same endpoint using curl it throws an error.
Reference : HTTP GET with request body