一、前言
众所周知,在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@ToStringstatic class User {private Integer age;private String name;}@Getter@Setter@ToStringstatic class Home {private String addr;private String phone;}@Getter@Setter@ToStringstatic 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);}@Overridepublic BufferedReader getReader() throws IOException {return new BufferedReader(new InputStreamReader(getInputStream()));}@Overridepublic ServletInputStream getInputStream() throws IOException {final ByteArrayInputStream bais = new ByteArrayInputStream(paramBody);return new ServletInputStream() {@Overridepublic int read() throws IOException {return bais.read();}@Overridepublic int available() throws IOException {return paramBody.length;}@Overridepublic boolean isFinished() {return false;}@Overridepublic boolean isReady() {return false;}@Overridepublic 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 {@Overridepublic 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 {@Beanpublic 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@ToStringstatic class User {private Integer age;private String name;}@Getter@Setter@ToStringstatic class Home {private String addr;private String phone;}@Getter@Setter@ToStringstatic 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请求:
通过测试结果可以看到,此时多个@RequestBody修饰参数进行请求体接收的功能就实现啦。