Spring MVC @RestController和redirect
我有一个使用Spring MVC @RestController实现的REST端点。 有时,取决于我的控制器中的input参数我需要在客户端发送httpredirect。
是否有可能与Spring MVC @RestController,如果是这样,你可以请示例?
 添加一个HttpServletResponse参数到你的Handler方法,然后调用response.sendRedirect("some-url"); 
就像是:
 @RestController public class FooController { @RequestMapping("/foo") void handleFoo(HttpServletResponse response) throws IOException { response.sendRedirect("some-url"); } } 
 为了避免对HttpServletRequest或HttpServletResponse直接依赖,我build议一个“纯Spring”实现返回一个ResponseEntity,如下所示: 
 HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create(newUrl)); return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY); 
 如果您的方法总是返回一个redirect,请使用ResponseEntity<Void> ,否则通常返回为genericstypes。 
如果你@RestController返回一个string,你可以使用这样的东西
 return "redirect:/other/controller/"; 
而这种redirect只适用于GET请求,如果你想使用其他types的请求,使用HttpServletResponse