使用@RequestBody注释或@ResponseBody进行春季升级



我是春天的新手。我想使用@RequestBody注释或@ResponseBody注释,而不是使用ModelView。以下是我要使用的当前具有模型和视图的方法 @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;
}

如何在没有ModelView的情况下使用指定的注释。

我想你不明白,@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 开始有一个关于它的部分。

希望这是有帮助的。

最新更新