Spring forward和redirect
什么是forward
forward表示转发,当请求到来时,可以将请求转发到其他指定的服务,而调用方对此并无感知,如下图:
forward示例
package com.yuxiao.springbootadmin.client.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.Serializable;
/**
* @author yangjunwei
* @date 2021-04-13 11:39
*/
@Controller
public class ForwardController {
/**
* POST请求时,转发者不能接受请求体数据,转发后的方法无法获取到请求体数据而出错
* @return
*/
@PostMapping("/forward/post")
public String forwardPost(/*@RequestBody User user*/){
return "forward:/target/post";
}
@PostMapping("/target/post")
@ResponseBody
public String targetPost(@RequestBody User user){
return user.toString();
}
/**
* GET请求时,此处对参数是否接受是可选的
* @return
*/
@GetMapping("/forward/get")
public String forwardGet(/*User user*/){
return "forward:/target/get";
}
@ResponseBody
@GetMapping("/target/get")
public String targetGet(User user){
return user.toString();
}
}
class User implements Serializable {
private static final long serialVersionUID = -5214177627251919784L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
forward注意事项
- 转发和被转发的请求类型必须一致,不能混用。即全是GET或全是POST;
- 转发者方法不能被标识为
@RestController
或者@ResponseBody
; - POST请求时,转发者不能接受请求体,否则被转发者无法获取到请求体数据(请求体是数据流,流只能读取一次);
什么是redirect
redirect表示请求转发,即请求发给A服务时,A服务返回重定向给客户端,告诉客户端去请求B服务,如下图:
redirect示例
package com.yuxiao.springbootadmin.client.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;
/**
* redirect测试
* @author yangjunwei
* @date 2021-04-14 20:37
*/
@Controller
public class RedirectController {
/**
* Post不支持redirect,无法重定向到指定的接口上
* @return
*/
@PostMapping("/redirect/post")
public String redirectPost(){
return "redirect:/target/post";
}
@GetMapping("/redirect/get")
public String redirectGet(HttpServletRequest request) throws UnsupportedEncodingException {
Enumeration<String> parameterNames = request.getParameterNames();
StringBuilder urlParam = new StringBuilder();
while (parameterNames.hasMoreElements()){
String paramName = parameterNames.nextElement();
// 需要对值进行URL编码,避免参数为中文时的乱码问题
urlParam.append(paramName).append("=").append(URLEncoder.encode(request.getParameter(paramName), "UTF-8")).append("&");
}return "redirect:/target/get/?"+urlParam.toString();
}
}
redirect注意事项
- redirect不支持post方式的请求,即转发者接收到请求后,无法让客户端重定向到被转发的接口上;
- redirect时,需要携带请求参数,由于参数是在URL中传递,需要对URL中的值进行URL编码,避免中文乱码;