原始的controller,注意,只能使用@Controller
注解,不能够使用@RestController
注解,因为@RestController
是包含了@ResponseBody
的。
@Controller
public class ForwardAndRedirectController {
@GetMapping("/forward")
public String forward() {
System.out.println("forward........");
return "forward:/gethello";
}
@GetMapping("/redirect")
public String redirect() {
System.out.println("redirect.......");
return "redirect:/gethello";
}
}
被转发或重定向的controller,可以@RestController
:
@GetMapping("/gethello")
public String getHello() {
return "hello weixin";
}
当请求forward:
http://localhost:8080/forward
结果如下:
当请求redirect
http://localhost:8080/redirect
结果如下:
可见url的地址已经发生了跳转,这个就是重定向。