AdSense

網頁

2021/2/4

Spring Boot 重新導向新網址 redirect url

Spring Boot 重新導向到別的網址的方式如下。

使用RedirectView可重新導向到指定的url。

package com.abc.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.view.RedirectView;

@Controller
public class DemoController {

    @GetMapping("/redirect")
    public RedirectView redirect() {

        String url = "https://matthung0807.blogspot.com"

        return new RedirectView(url); // 重新導向到指定的url
    }

}

或是ModelAndView傳入"redirect:"前墜加上要導向的url字串。

package com.abc.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DemoController {

    @GetMapping("/redirect")
    public ModelAndView redirect() {
        String url ="https://matthung0807.blogspot.com";
        return new ModelAndView("redirect:" + url); // 重新導向到指定的url
    }

}

或是回傳"redirect:"前墜加上要導向的url字串。

package com.abc.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class DemoController {

    @GetMapping("/redirect")
    public String redirect() {
        String url ="https://matthung0807.blogspot.com";
        return "redirect:" + url; // 重新導向到指定的url
    }

}

或是透過HttpServletResponse設定response header的Location值為重新導向的url及HTTP狀態碼為302 Found(RFC 7231 - 6.4.3)。

package com.abc.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;

@Controller
public class DemoController {

    @GetMapping("/redirect")
    public void redirect(HttpServletResponse response) {
        String url = "https://matthung0807.blogspot.com";
        response.setHeader("Location", url); // 重新導向的url
        response.setStatus(HttpServletResponse.SC_FOUND); // 302 Found
    }

}

重新導向的過程如下:

                                                  localhost        blogger
+--------+                                        +--------+       +--------+
| Client |                                        | Server |       | Server |
+--------+                                        +--------+       +--------+
     |                                                |                |
     |                                                |                |
     |GET http:localhost:8080/redirect HTTP/1.1       |                |
     +----------------------------------------------->+                |
     |                                                |                |
     |                                                |                |
     |     HTTP/1.1 302                               |                |
     |     Location: https://matthung0807.blogspot.com|                |
     +<-----------------------------------------------+                |
     |                                                |                |
     |                                                |                |
     |                                                |                |
     |GET https://matthung0807.blogspot.com HTTP/1.1  |                |
     +---------------------------------------------------------------->+
     |                                                |                |
     |                                                |                |
     |                                                |                |
     |                                 HTTP/1.1 200 OK|                |
     +<----------------------------------------------------------------+
     |                                                |                |
     |                                                |                |
     |                                                |                |
     v                                                v                v


沒有留言:

AdSense