301,302重定向
2021-09-14 14:02:44
302临时重定向:
第一种:
@Controller @RequestMapping(path = "redirect") public class RedirectRest { @ResponseBody @GetMapping(path = "index") public String index(HttpServletRequest request) { return "重定向访问! " + JSON.toJSONString(request.getParameterMap()); } @GetMapping(path = "r1") public String r1() { return "redirect:/redirect/index?base=r1"; } }
第二种:
@ResponseBody @GetMapping(path = "r2") public void r2(HttpServletResponse response) throws IOException { response.sendRedirect("/redirect/index?base=r2"); }
301永久重定向:
@GetMapping(value = "/index") public void index( HttpServletRequest request, HttpServletResponse response) throws IOException { //301 String newUrl = request.getContextPath() + "/"; response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); response.setHeader("Location", newUrl); response.setHeader("Connection", "close"); }