【问题标题】:Spring Upgrade using @RequestBody annotation or @ResponseBody使用 @RequestBody 注释或 @ResponseBody 进行 Spring 升级
【发布时间】:2014-12-18 17:34:53
【问题描述】:

我是 Spring 新手。我想使用@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如何使用指定的注解。

【问题讨论】:

    标签: spring-mvc annotations


    【解决方案1】:

    我想你没明白,@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 有一节是关于它的。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2016-12-30
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      相关资源
      最近更新 更多