SpringBoot请求转发的方式

概论

想要使用SpringBoot进行请求的转发,我们一共是有两大类(四种方法),一种是controller控制器转发一种是使用HttpServletRequest进行转发,这里每个方式都有两种转发方式一种内部转发一种外部转发

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,并且注意外部转发和内部转发的优缺点。文章来源地址https://uudwc.com/A/gVV9O

原文地址:https://blog.csdn.net/qq_45153375/article/details/131935804

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

上一篇 2023年09月06日 11:54
C#实战:基于腾讯OCR技术实现企业证书识别和数据提取实践
下一篇 2023年09月06日 11:54