在spring mvc中的控制器方法之间传递数据

时间:2018-04-14 07:35:05

标签: java spring spring-mvc model-view-controller

我使用spring MVC来调用restful web服务,我必须将一个服务数据传递给其他服务,比如bellow,请让我知道最好的&通过日期的安全方式。

Code Example:

@RequestMapping(value = "/verify", method = RequestMethod.GET)
public ModelAndView getToken() {
    ModelAndView model = new ModelAndView();
    model.addObject(new VerifyIdentityBean());
    model.setViewName("verifyIdentity");
    return model;
}

@RequestMapping(value = "/verify", method = RequestMethod.POST)
    public ModelAndView getTokenPost() {
       **// Here I will call one web service by passing form input
      // I need to pass that service repose to next form post method**
        ModelAndView model = new ModelAndView();
        model.setViewName("redirect:/userinfo");
        return model;
}

@RequestMapping(value = "/userinfo", method = RequestMethod.GET)
public ModelAndView getuser() {
    ModelAndView model = new ModelAndView();
    model.addObject(new UserBean());
    model.setViewName("userinfo");
    return model;
}

@RequestMapping(value = "/userinfo", method = RequestMethod.POST)
    public ModelAndView getUserPost() {
       **//Here I want first post method service response to pass input to next web service
       // Here I will call one web service by passing above input**
    return model;
}

1 个答案:

答案 0 :(得分:0)

我认为有必要通过model.setViewName("redirect:/userinfo");调用网络方法。因为他们是同一个服务,为什么不直接打电话。

@AutoWired
CustomService service;

@RequestMapping(value = "/verify", method = RequestMethod.POST)
public ModelAndView getTokenPost() {
    ModelAndView model = service.getUserPost();
    // do some modify.
    return model;

}

我们通常在界面中定义restful api,即使您不需要界面。我们通常在另一个服务层实现Web服务。这有两个好处: 1.从控制器和服务中解耦。 2.该服务是可重复使用的。像上面的代码一样。