在Spring MVC Controller中返回302状态代码

Gen*_*tto 4 spring http-status-code-302

我正在开发一个Spring MVC应用程序,我需要在我的控制器中检查一定的条件.如果是真的,我必须返回302状态代码.它是这样的:

@RequestMapping(value = "/mypath.shtml", method = RequestMethod.GET)
public ModelAndView pageHandler(@Valid MyForm form, BindingResult result, HttpServletRequest request,
        Locale locale) throws PageControllerException, InvalidPageContextException, ServiceException {

    if (someCondition){
        // return 302 status code
    }
    else{
        // Do some stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

这是最好的方法吗?

非常感谢你提前

Gen*_*tto 5

我终于设法使用了@ResponseStatus,如下所示:

/sf/answers/144693041/

UPDATE

这是我最终做到的方式:

@ResponseStatus(value = HttpStatus.MOVED_TEMPORARILY)
public class MovedTemporarilyException extends RuntimeException {

    // ...
}

@RequestMapping(value = "/mypath.shtml", method = RequestMethod.GET)
public ModelAndView pageHandler(@Valid MyForm form, BindingResult result, HttpServletRequest request,
        Locale locale) throws PageControllerException, InvalidPageContextException, ServiceException {

    if (someCondition){
        throw new MovedTemporarilyException();
    }
    else{
        // Do some stuff
    }
}
Run Code Online (Sandbox Code Playgroud)