@Test public void userAddTest() throws Exception { User user = new User(); user.setName("测试姓名"); user.setAge(22); Gson gson = new GsonBuilder().setPrettyPrinting().create(); MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/user") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(gson.toJson(user)) .accept(MediaType.APPLICATION_JSON)) .andReturn(); MockHttpServletResponse response = mvcResult.getResponse(); System.out.println(response.getStatus()); System.out.println(response.getContentAsString()); }
@PostMapping(value = "/user") public User userAdd(@RequestBody User userParams) throws Exception { User user = new User(); user.setName(userParams.getName()); user.setAge(userParams.getAge()); if (userParams.getName().length() > 4) { throw new UserException(MessageEnum.NAME_EXCEEDED_CHARRACTER_LIMIT); } return userRepository.save(user); }
UserController { @PostMapping(value = "/user") public User userAdd(@RequestBody User userParams) throws Exception { User user = new User(); user.setName(userParams.getName()); user.setAge(userParams.getAge()); if (userParams.getName().length() > 4) { throw new UserException(MessageEnum.NAME_EXCEEDED_CHARRACTER_LIMIT); } return userRepository.save(user); } }
UserController { @PostMapping(value = "/user") public User userAdd(@RequestBody User userParams) throws Exception { User user = new User(); user.setName(userParams.getName()); user.setAge(userParams.getAge()); if (userParams.getName().length() > 4) { throw new UserException(MessageEnum.NAME_EXCEEDED_CHARRACTER_LIMIT); } return userRepository.save(user); }  }
UserController { @PostMapping(value = "/user") public User userAdd(@RequestBody User userParams) throws Exception { User user = new User(); user.setName(userParams.getName()); user.setAge(userParams.getAge()); if (userParams.getName().length() > 4) { throw new UserException(MessageEnum.NAME_EXCEEDED_CHARRACTER_LIMIT); } return userRepository.save(user); }  @PostMapping(value = "/user") User userAdd(@RequestBody User userParams); @RequestMapping(value = "/user/list/{exception}") Result<User> userList(@PathVariable("exception") Boolean exception); @RequestMapping(value = "/user/{id}") Result<User> userFindOne(@PathVariable("id") Integer id); @RequestMapping(value = "/user/name", method = RequestMethod.GET) List<User> findUserListByName(@RequestParam(name="name",defaultValue="") String name); @PutMapping(value = "/user/{id}") User userUpdate(
            @PathVariable("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("age") Integer age
    ); @DeleteMapping(value = "/user/{id}") Result deleteUser(@PathVariable("id") Integer id); }
UserController { @PostMapping(value = "/user") public User userAdd(@RequestBody User userParams) throws Exception { User user = new User(); user.setName(userParams.getName()); user.setAge(userParams.getAge()); if (userParams.getName().length() > 4) { throw new UserException(MessageEnum.NAME_EXCEEDED_CHARRACTER_LIMIT); } return userRepository.save(user); }  @PostMapping(value = "/user") User userAdd(@RequestBody User userParams); @RequestMapping(value = "/user/list/{exception}") Result<User> userList(@PathVariable("exception") Boolean exception); @RequestMapping(value = "/user/{id}") Result<User> userFindOne(@PathVariable("id") Integer id); @RequestMapping(value = "/user/name", method = RequestMethod.GET) List<User> findUserListByName(@RequestParam(name="name",defaultValue="") String name); @PutMapping(value = "/user/{id}") User userUpdate(
            @PathVariable("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("age") Integer age
    ); @DeleteMapping(value = "/user/{id}") Result deleteUser(@PathVariable("id") Integer id);  }