[英]Spring Boot URL Mapping
我试图在线研究这个特定问题,但我真的不知道在 Google 上搜索什么。 我的问题是:有没有办法将 url 自动重定向到另一个 url。 For example: I type http://localhost:8080
which directs you to http://localhost:8080/
I was wondering if I could make it so http://localhost:8080
redirect to http://localhost:8080/home
使用以下状态HttpStatus.PERMANENT_REDIRECT
指示此 URL( /
) 已永久移动,并使用Location
header 指示新位置 ( /home
) 是什么。
public class GreetingController {
@GetMapping("/")
public ResponseEntity<Map<String, String>> sayHello1() {
return ResponseEntity.status(HttpStatus.PERMANENT_REDIRECT)
.header("Location", "/home").build();
}
@GetMapping("/home")
public ResponseEntity<Map<String, String>> sayHello() {
return ResponseEntity.ok()
.body(Collections.singletonMap("message", "Hello world!"));
}
}
希望这能解决您的问题。
这对我有用。
@RestController
@RequestMapping("")
public class TestController {
@GetMapping(value = "")
public ModelAndView redirect(HttpServletRequest request) {
String redirectUrl = request.getRequestURL().toString() + "home";
return new ModelAndView("redirect:" + redirectUrl);
}
}
public class TestController {
@GetMapping(value = "/")
public String home() {
return "redirect:/home";
}
}
您不应将@ResponseBody注释放在方法定义中,否则重定向将不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.