使用@RequestBody将请求体映射到Action方法参数中

时间:2024-01-10 22:10:20
     @PostMapping("/user")
public User create(@RequestBody User user){
System.out.println(user.toString());
user.setId(5l);
return user;
}
     @Test
public void whenCreateSuccess() throws Exception {
String content = "{\"username\":\"fanqi\",\"password\":\"admin\",\"enabled\":\"1\"}";
mockMvc.perform(MockMvcRequestBuilders.post("/user")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(content))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").exists());
}