Springboot请求转发的方式

controller控制器转发 

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";
    }
}


 

切记转发不能使用RestController要不然不会被view解析会直接返回对应的字符串到页面 

HttpServleRequest转发 

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 "收到转发的请求";
    }

    @RequestMapping("/ghr2")
    public String ghr2(){
        return "ghr2收到转发完毕";
    }
}




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 "转发完毕";
    }

    @RequestMapping("/thr2")
    public String r2(HttpServletResponse httpServletResponse) throws IOException {
        httpServletResponse.sendRedirect("/ghr2");
        return "转发完毕!";
    }
}





注意到底是HttpServleRequest还是HttpServleResponse,并且注意外部转发和内部转发的优缺点。

SpringBoot请求转发的方式_spring boot_堕落年代-北京城市开发者社区SpringBoot进行请求转发 堕落年代 北京城市开发者社区icon-default.png?t=N7T8https://devpress.csdn.net/beijing/64f7435787b26b6585a1e10f.html?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6Mjk3OTk5MiwiZXhwIjoxNjk1OTAwNTIxLCJpYXQiOjE2OTUyOTU3MjEsInVzZXJuYW1lIjoiWVcxMDI4In0.OHiigXy1p167jqVaLqPC_QXeIojKWbdy68QA_zR6Soo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值