我是春天的新手。我想使用@RequestBody
注释或@ResponseBody
注释,而不是使用Model
和View
。以下是我要使用的当前具有模型和视图的方法 @ResponseBody
注释
@RequestMapping(value="/user/limits", method=RequestMethod.GET)
protected ModelAndView getUserLimits(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) throws Exception {
ModelAndView mv = new ModelAndView();
mv.setView(new MappingJackson2JsonView());
if(authentication == null){
response.setStatus( HttpServletResponse.SC_FORBIDDEN);
return null;
}
String userid = authentication.getName();
mv.addObject("limits", Service.getlimitsInfo(userid));
return mv;
}
如何在没有Model
和View
的情况下使用指定的注释。
我想你不明白,@ResponseBody和@RequestBody的实际意义。
@ResponseBody @RequestMapping("/description")
public Description getDescription(@RequestBody UserStats stats){
return new Description(stats.getFirstName() + " " + stats.getLastname() + " hates wacky wabbits");
}
您的方法标头如下所示:
protected @ReponseBody List<Limit> getUserLimits(HttpServletRequest request,
HttpServletResponse response, @RequestBody Authentication authentication) throws Exception
然后,您可以将它与任何javascript库一起使用,将数据作为json发布,并将它们作为json获取。
您也可以从阅读 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html 开始有一个关于它的部分。
希望这是有帮助的。