[英]Spring Boot URL Mapping
I was trying to research this specific problem online but I don't really know what to search up on Google.我试图在线研究这个特定问题,但我真的不知道在 Google 上搜索什么。 My question is: is there a way to auto-redirect a url to another url.我的问题是:有没有办法将 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
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
Use the following status HttpStatus.PERMANENT_REDIRECT
to indicate that this URL( /
) is permanently moved and use Location
header to indicate what is the new location( /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!"));
}
}
Hope this will solve your problem.希望这能解决您的问题。
this is works for me.这对我有用。
@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";
}
}
You should not put @ResponseBody annotation in method definition else the redirect will not work.您不应将@ResponseBody注释放在方法定义中,否则重定向将不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.