SpringBoot請求轉(zhuǎn)發(fā)的方式小結(jié)
概論
想要使用SpringBoot進行請求的轉(zhuǎn)發(fā),我們一共是有兩大類(四種方法),一種是controller控制器轉(zhuǎn)發(fā)一種是使用HttpServletRequest進行轉(zhuǎn)發(fā),這里每個方式都有兩種轉(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 "收到請求rt1";
}
@RequestMapping("/r2")
public String r2(){
return "收到請求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要不然不會被view解析會直接返回對應(yīng)的字符串到頁面
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ā)的請求";
}
@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)缺點。
到此這篇關(guān)于SpringBoot請求轉(zhuǎn)發(fā)的方式小結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot請求轉(zhuǎn)發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)InputStream的任意拷貝方式
這篇文章主要介紹了Java實現(xiàn)InputStream的任意拷貝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java實戰(zhàn)之基于TCP實現(xiàn)簡單聊天程序
這篇文章主要為大家詳細介紹了如何在Java中基于TCP實現(xiàn)簡單聊天程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
java實現(xiàn)遍歷樹形菜單兩種實現(xiàn)代碼分享
這篇文章主要介紹了java實現(xiàn)遍歷樹形菜單兩種實現(xiàn)代碼分享,兩種實現(xiàn):OpenSessionView實現(xiàn)、TreeAction實現(xiàn)。具有一定參考價值,需要的朋友可以了解下。2017-11-11
解決RestTemplate 的getForEntity調(diào)用接口亂碼的問題
這篇文章主要介紹了解決RestTemplate 的getForEntity調(diào)用接口亂碼的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

