I was just playing with the spring-boot, just wanted to create a controller with method = RequestMethod.POST
my controller:
@RequestMapping(value = "/user/signup",
method = RequestMethod.POST)
private String signUpNewUser(@Valid @RequestBody SignInUserForm userForm){
// validation + logic
}
SignUpUserForm class:
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class SignInUserForm {
@NotEmpty
@Email
private String email;
@NotEmpty
@Size(min = 8, max = 24)
private String password;
@NotEmpty
@Size(min = 8, max = 24)
private String repeatedPassword;
}
and finally my test:
@Test
public void shouldCallPostMethod() throws Exception {
SignInUserForm signInUserForm = new SignInUserForm("test@mail.com", "aaaaaaaa", "aaaaaaaa");
String json = new Gson().toJson(signInUserForm);
mockMvc.perform(
MockMvcRequestBuilders.post("/user/signup")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(json))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isCreated());
}
as far as the SignUpControllerForm contain no-args constructor, everything works fine, but once it is missing this is what i receive from the MockMvcResultHandlers.print()
:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /user/signup
Parameters = {}
Headers = {Content-Type=[application/json]}
Handler:
Type = org.bitbucket.akfaz.gui.controller.SignUpController
Method = private java.lang.String org.bitbucket.akfaz.gui.controller.SignUpController.signUpNewUser(org.bitbucket.akfaz.gui.model.SignInUserForm)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.http.converter.HttpMessageNotReadableException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
MockHttpServletResponse:
Status = 400
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
What I want to express is that, the exception HttpMessageNotReadableException
is not descriptive enough. Shouldn't be there any exception connected with the @RequestBody? It would save a lot of time.
And how exactly Spring converts JSON to java object with no-arg constructor (it does not uses the getters as i checked)?