springboot 直接转发调用_springboot实现转发和重定向

时间:2025-03-28 20:57:21

1、转发

方式一:使用 "forward" 关键字(不是指java关键字),注意:类的注解不能使用@RestController 要用@Controller

@RequestMapping(value="/test/test01/{name}" , method = )

public String test(@PathVariable String name) {

return "forward:/ceng/";

}

方式二:使用servlet 提供的API,注意:类的注解可以使用@RestController,也可以使用@Controller

@RequestMapping(value="/test/test01/{name}" , method = )

public void test(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) throws Exception {

("/ceng/").forward(request,response);

}

2、重定向

方式一:使用 "redirect" 关键字(不是指java关键字),注意:类的注解不能使用@RestController,要用@Controller

@RequestMapping(value="/test/test01/{name}" , method = )

public String test(@PathVariable String name) {

return "redirect:/ceng/";

}

方式二:使用servlet 提供的API,注意:类的注解可以使用@RestController,也可以使用@Controller

@RequestMapping(value="/test/test01/{name}" , method = )

public void test(@PathVariable String name, HttpServletResponse response) throws IOException {

("/ceng/");

}

使用API进行重定向时,一般会在url之前加上:()