SpringBoot請(qǐng)求轉(zhuǎn)發(fā)的方式小結(jié)
概論
想要使用SpringBoot進(jìn)行請(qǐng)求的轉(zhuǎn)發(fā),我們一共是有兩大類(四種方法),一種是controller控制器轉(zhuǎn)發(fā)一種是使用HttpServletRequest進(jìn)行轉(zhuǎn)發(fā),這里每個(gè)方式都有兩種轉(zhuǎn)發(fā)方式一種內(nèi)部轉(zhuǎn)發(fā)一種外部轉(zhuǎn)發(fā)
controller控制器轉(zhuǎn)發(fā)
package com.example.requestplay.demos.web.RequestPlay1;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author:DUOLUONIANDAI
* @DATA:2023/07/26 11:24
* @Title:
*/
@RestController
public class GetPlay {
@RequestMapping("/r1")
public String r1(){
return "收到請(qǐng)求rt1";
}
@RequestMapping("/r2")
public String r2(){
return "收到請(qǐng)求rt2";
}
}package com.example.requestplay.demos.web.RequestPlay1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author:DUOLUONIANDAI
* @DATA:2023/07/26 11:24
* @Title:
*/
@Controller
public class ToGetPlay {
@RequestMapping("/tr1")
public String r1(){
return "forward:/r1";
}
@RequestMapping("/tr2")
public String r2(){
return "redirect:/r2";
}
}切記轉(zhuǎn)發(fā)不能使用RestController要不然不會(huì)被view解析會(huì)直接返回對(duì)應(yīng)的字符串到頁(yè)面
HttpServleRequest轉(zhuǎn)發(fā)
package com.example.requestplay.demos.web.RequestPlay2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author:DUOLUONIANDAI
* @DATA:2023/07/26 11:40
* @Title:
*/
@RestController
public class GetRequest {
@RequestMapping("/ghr1")
public String ghr1(){
return "收到轉(zhuǎn)發(fā)的請(qǐng)求";
}
@RequestMapping("/ghr2")
public String ghr2(){
return "ghr2收到轉(zhuǎn)發(fā)完畢";
}
}package com.example.requestplay.demos.web.RequestPlay2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author:DUOLUONIANDAI
* @DATA:2023/07/26 11:39
* @Title:
*/
@RestController
public class ToRequest {
@RequestMapping("/thr1")
public String r1(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
RequestDispatcher requestDispatcher = httpServletRequest.getRequestDispatcher("/ghr1");
requestDispatcher.forward(httpServletRequest,httpServletResponse);
return "轉(zhuǎn)發(fā)完畢";
}
@RequestMapping("/thr2")
public String r2(HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.sendRedirect("/ghr2");
return "轉(zhuǎn)發(fā)完畢!";
}
}注意到底是HttpServleRequest還是HttpServleResponse,并且注意外部轉(zhuǎn)發(fā)和內(nèi)部轉(zhuǎn)發(fā)的優(yōu)缺點(diǎn)。
到此這篇關(guān)于SpringBoot請(qǐng)求轉(zhuǎn)發(fā)的方式小結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot請(qǐng)求轉(zhuǎn)發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)InputStream的任意拷貝方式
這篇文章主要介紹了Java實(shí)現(xiàn)InputStream的任意拷貝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java實(shí)戰(zhàn)之基于TCP實(shí)現(xiàn)簡(jiǎn)單聊天程序
這篇文章主要為大家詳細(xì)介紹了如何在Java中基于TCP實(shí)現(xiàn)簡(jiǎn)單聊天程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
java實(shí)現(xiàn)遍歷樹(shù)形菜單兩種實(shí)現(xiàn)代碼分享
這篇文章主要介紹了java實(shí)現(xiàn)遍歷樹(shù)形菜單兩種實(shí)現(xiàn)代碼分享,兩種實(shí)現(xiàn):OpenSessionView實(shí)現(xiàn)、TreeAction實(shí)現(xiàn)。具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Spring Boot(二)之web綜合開(kāi)發(fā)
本篇文章為大家介紹spring boot的其它特性(有些未必是spring boot體系桟的功能,但是是spring特別推薦的一些開(kāi)源技術(shù)本文也會(huì)介紹),對(duì)了這里只是一個(gè)大概的介紹,特別詳細(xì)的使用我們會(huì)在其它的文章中來(lái)展開(kāi)說(shuō)明2017-05-05
解決RestTemplate 的getForEntity調(diào)用接口亂碼的問(wèn)題
這篇文章主要介紹了解決RestTemplate 的getForEntity調(diào)用接口亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

