使用@RequestBody将请求体映射到Action方法参数中
1 @PostMapping("/user") 2 public User create(@RequestBody User user){ 3 System.out.println(user.toString()); 4 user.setId(5l); 5 return user; 6 }
1 @Test 2 public void whenCreateSuccess() throws Exception { 3 String content = "{\"username\":\"fanqi\",\"password\":\"admin\",\"enabled\":\"1\"}"; 4 mockMvc.perform(MockMvcRequestBuilders.post("/user") 5 .contentType(MediaType.APPLICATION_JSON_UTF8) 6 .content(content)) 7 .andExpect(status().isOk()) 8 .andExpect(jsonPath("$.id").exists()); 9 }