SpringBoot中的重定向是什么意思

本篇內(nèi)容介紹了“SpringBoot中的重定向是什么意思”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

10余年建站經(jīng)驗(yàn), 成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)客戶的見證與正確選擇。創(chuàng)新互聯(lián)建站提供完善的營銷型網(wǎng)頁建站明細(xì)報價表。后期開發(fā)更加便捷高效,我們致力于追求更美、更快、更規(guī)范。

I. 環(huán)境搭建

首先得搭建一個web應(yīng)用才有可能繼續(xù)后續(xù)的測試,借助SpringBoot搭建一個web應(yīng)用屬于比較簡單的活;

創(chuàng)建一個maven項(xiàng)目,pom文件如下

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7</version>
    <relativePath/> <!-- lookup parent from update -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.45</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

依然是一般的流程,pom依賴搞定之后,寫一個程序入口

/**
 * Created by @author yihui in 15:26 19/9/13.
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

II. 302重定向

1. 返回redirect

這種case通常適用于返回視圖的接口,在返回的字符串前面添加redirect:方式來告訴Spring框架,需要做302重定向處理

@Controller
@RequestMapping(path = "redirect")
public class RedirectRest {

    @ResponseBody
    @GetMapping(path = "index")
    public String index(HttpServletRequest request) {
        return "重定向訪問! " + JSON.toJSONString(request.getParameterMap());
    }

    @GetMapping(path = "r1")
    public String r1() {
        return "redirect:/redirect/index?base=r1";
    }
}

上面給出了一個簡單的demo,當(dāng)我們訪問/redirect/r1時,會重定向到請求/redirect/index?base=r1,實(shí)際測試結(jié)果如下

SpringBoot中的重定向是什么意思

注意上面的截圖,我們實(shí)際訪問的連接是 http://127.0.0.1:8080/redirect/index?base=r1,在瀏覽器中的表現(xiàn)則是請求url變成了http://127.0.0.1:8080/redirect/index?base=r1;通過控制臺查看到的返回頭狀態(tài)碼是302

說明

  • 使用這種方式的前提是不能在接口上添加@ResponseBody注解,否則返回的字符串被當(dāng)成普通字符串處理直接返回,并不會實(shí)現(xiàn)重定向

2. HttpServletResponse重定向

前面一篇說到SpringMVC返回?cái)?shù)據(jù)的時候,介紹到可以直接通過HttpServletResponse往輸出流中寫數(shù)據(jù)的方式,來返回結(jié)果;我們這里也是利用它,來實(shí)現(xiàn)重定向

@ResponseBody
@GetMapping(path = "r2")
public void r2(HttpServletResponse response) throws IOException {
    response.sendRedirect("/redirect/index?base=r2");
}

從上面的demo中,也可以看出這個的使用方式很簡單了,直接調(diào)用javax.servlet.http.HttpServletResponse#sendRedirect,并傳入需要重定向的url即可

SpringBoot中的重定向是什么意思

3. 小結(jié)

這里主要介紹了兩種常見的后端重定向方式,都比較簡單,這兩種方式也有自己的適用場景(當(dāng)然并不絕對)

  • 在返回視圖的前面加上redirect的方式,更加適用于視圖的跳轉(zhuǎn),從一個網(wǎng)頁跳轉(zhuǎn)到另一個網(wǎng)頁

  • HttpServletResponse#sendRedirec的方式更加靈活,可以在后端接收一次http請求生命周期中的任何一個階段來使用,比如有以下幾種常見的場景

    • 某個接口要求登錄時,在攔截器層針對所有未登錄的請求,重定向到登錄頁面

    • 全局異常處理中,如果出現(xiàn)服務(wù)器異常,重定向到定制的500頁面

    • 不支持的請求,重定向到404頁面

“SpringBoot中的重定向是什么意思”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

本文名稱:SpringBoot中的重定向是什么意思
文章鏈接:http://bm7419.com/article0/igccoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、定制網(wǎng)站網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)建站公司、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司