public class JsonView { public static ModelAndView Render(Object model, HttpServletResponse response) { MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter(); MediaType jsonMimeType = MediaType.APPLICATION_JSON; try { jsonConverter.write(model, jsonMimeType, new ServletServerHttpResponse(response)); } catch (HttpMessageNotWritableException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
这是实现方法,下边是调用方法:
@RequestMapping(value="/users", method=RequestMethod.POST)public Object index(@RequestBody SearchUsersViewModel model, HttpServletResponse response) { model.setList(userService.getUsers(model)); if(true) { return new ModelAndView("controls/tables/users", "model", model); } else { return JsonView.Render(model, response); } }