如何使用 Spring 引导重定向到新域 (url)?
How to redirect to a new domain(url) using Spring Boot?
我正在使用 spring 启动构建一个 link 缩短器。除了将用户重定向到他们的 url 之外,一切正常。我检查了 Whosebug 上的其他示例,但似乎对我没有任何作用。
请在下面查看我的代码
public class DosuController {
private final DosuService dosuService;
public DosuController(DosuService dosuService) {
this.dosuService = dosuService;
}
@GetMapping("/{dosu}")
public ResponseEntity<Void> redirect(@PathVariable("dosu") String dosu) {
return dosuService.redirect(dosu);
}
}
//SERVICE
public class DosuService {
private final DosuRepo dosuRepo;
public DosuService(DosuRepo dosuRepo) {
this.dosuRepo = dosuRepo;
}
public ResponseEntity<Void> redirect(String dosu) {
Dosu dosus = dosuRepo.findByDosu(dosu);
if (dosus == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Dosu not found with name: +"dosu);
}
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create(dosus.getRedirectUrl())
.build();
}
}
输出:localhost:8080/www.google.com
期望输出:www.google.com
您应该先检查您的 dosus.getRedirectUrl()
。这真的是 url 您所期望的吗?
然后,重构您的代码,并尝试从您的控制器 return a redirect:url
。
控制器:
@GetMapping("/{dosu}")
public String redirect(@PathVariable("dosu") String dosu) {
Dosu dosus = dosuService.findByDosu(dosu);
if (dosus == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Dosu not found with name: " + dosu);
}
return "redirect:" + dosus.getRedirectUrl();
}
服务:
public Dosu findByDosu(String dosu) {
return dosuRepo.findByDosu(dosu);
}
您可以在 headers 位置设置 URI
,如以下代码片段所述。确保您应该添加正确的 URI SCHEME
(如 uri 中提到的 https)。例如:
public ResponseEntity<Void> redirect() {
HttpHeaders headers = new HttpHeaders();
headers.setLocation(URI.create("https://www.google.com"));
return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY);
}
更新服务class方法
public ResponseEntity<Void> redirect(String dosu){
Dosu dosus = dosuRepo.findByDosu(dosu);
if(dosus == null){
throw new ResponseStatusException (HttpStatus.NOT_FOUND,"Dosu not found with name: +"dosu);
}
HttpHeaders headers = new HttpHeaders();
// Make sure uri should be valid uri with proper scheme name
headers.setLocation(URI.create(dosus.getRedirectUrl()));
return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY);
}
我正在使用 spring 启动构建一个 link 缩短器。除了将用户重定向到他们的 url 之外,一切正常。我检查了 Whosebug 上的其他示例,但似乎对我没有任何作用。
请在下面查看我的代码
public class DosuController {
private final DosuService dosuService;
public DosuController(DosuService dosuService) {
this.dosuService = dosuService;
}
@GetMapping("/{dosu}")
public ResponseEntity<Void> redirect(@PathVariable("dosu") String dosu) {
return dosuService.redirect(dosu);
}
}
//SERVICE
public class DosuService {
private final DosuRepo dosuRepo;
public DosuService(DosuRepo dosuRepo) {
this.dosuRepo = dosuRepo;
}
public ResponseEntity<Void> redirect(String dosu) {
Dosu dosus = dosuRepo.findByDosu(dosu);
if (dosus == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Dosu not found with name: +"dosu);
}
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create(dosus.getRedirectUrl())
.build();
}
}
输出:localhost:8080/www.google.com
期望输出:www.google.com
您应该先检查您的 dosus.getRedirectUrl()
。这真的是 url 您所期望的吗?
然后,重构您的代码,并尝试从您的控制器 return a redirect:url
。
控制器:
@GetMapping("/{dosu}")
public String redirect(@PathVariable("dosu") String dosu) {
Dosu dosus = dosuService.findByDosu(dosu);
if (dosus == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Dosu not found with name: " + dosu);
}
return "redirect:" + dosus.getRedirectUrl();
}
服务:
public Dosu findByDosu(String dosu) {
return dosuRepo.findByDosu(dosu);
}
您可以在 headers 位置设置 URI
,如以下代码片段所述。确保您应该添加正确的 URI SCHEME
(如 uri 中提到的 https)。例如:
public ResponseEntity<Void> redirect() {
HttpHeaders headers = new HttpHeaders();
headers.setLocation(URI.create("https://www.google.com"));
return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY);
}
更新服务class方法
public ResponseEntity<Void> redirect(String dosu){
Dosu dosus = dosuRepo.findByDosu(dosu);
if(dosus == null){
throw new ResponseStatusException (HttpStatus.NOT_FOUND,"Dosu not found with name: +"dosu);
}
HttpHeaders headers = new HttpHeaders();
// Make sure uri should be valid uri with proper scheme name
headers.setLocation(URI.create(dosus.getRedirectUrl()));
return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY);
}