一、前言
众所周知,在Spring Boot框架中,Controller层API接口编码获取请求体参数时,在参数上会使用@RequestBody注解;如果一次请求中,请求体参数携带的内容需要用多个参数接收时,能不能多次使用@RequestBody注解呢?
下面我们先测试一下,参考代码:
package com.learn.springboot.controller;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestBodyController {
@Getter
@Setter
@ToString
static class User {
private Integer age;
private String name;
}
@Getter
@Setter
@ToString
static class Home {
private String addr;
private String phone;
}
@Getter
@Setter
@ToString
static class Car {
private String number;
}
@RequestMapping("/req/test")
public String test(@RequestBody User user, @RequestBody Home home, @RequestBody Car car) {
return new StringBuilder(user.toString()).append("--").append(home.toString()).append("--").append(car.toString()).toString();
}
}
PostMan进行请求:
服务端后端日志:
由上面的测试代码可以看出,Spring Boot框架原生是不支持多个参数使用@RequestBody注解的,那么要怎么做才能支持呢?
二、Spring Boot支持多个@RequestBody注解接收参数
1. 增加HttpServletRequest对象输入流获取参数逻辑适配器
import cn.hutool.core.io.IoUtil;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class MultiRequestBodyWrapper extends HttpServletRequestWrapper {
private final byte[] paramBody;
public MultiRequestBodyWrapper(HttpServletRequest request, ServletResponse response) throws IOException {
super(request);
request.setCharacterEncoding(StandardCharsets.UTF_8.name());
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
paramBody = IoUtil.readBytes(request.getInputStream(), false);
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(paramBody);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return bais.read();
}
@Override
public int available() throws IOException {
return paramBody.length;
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
}
}
2. 定义请求过滤器,使定义的适配器生效
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class MultiRequestBodyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
// 拦截请求格式是JSON格式的报文
if (request instanceof HttpServletRequest
&& StringUtils.startsWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE)) {
requestWrapper = new MultiRequestBodyWrapper((HttpServletRequest) request, response);
}
if (null == requestWrapper) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
}
3. 配置过滤器,使自定义Filter生效
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MultiRequestBodyFilterConfig {
@Bean
public FilterRegistrationBean multiRequestBodyFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MultiRequestBodyFilter());
registration.addUrlPatterns("/req/*");
registration.setName("multiRequestBodyFilter");
return registration;
}
}
4. 重启应用,功能测试
代码:
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestBodyController {
@Getter
@Setter
@ToString
static class User {
private Integer age;
private String name;
}
@Getter
@Setter
@ToString
static class Home {
private String addr;
private String phone;
}
@Getter
@Setter
@ToString
static class Car {
private String number;
}
@RequestMapping("/req/test")
public String test(@RequestBody User user, @RequestBody Home home, @RequestBody Car car) {
return new StringBuilder(user.toString()).append("--").append(home.toString()).append("--").append(car.toString()).toString();
}
}
PostMan请求:
文章来源:https://www.toymoban.com/news/detail-797869.html
通过测试结果可以看到,此时多个@RequestBody修饰参数进行请求体接收的功能就实现啦。文章来源地址https://www.toymoban.com/news/detail-797869.html
到了这里,关于Spring Boot框架中Controller层API接口如何支持使用多个@RequestBody注解接受请求体参数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!