背景:因为RequestBody是以流的形式读取,流读取一次以后就没有了,所以HttpServletRequest 的 getInputStream() 和 getReader() 都只能读取一次。如果想要通过WebRequst再次获取RequestBody中的参数,首先需将RequestBody保存,然后通过自定义HttpServletRequestWrapper类,重写当中getReader()和getInputStream()方法;然后再通过Filter中将ServletRequest替换为自定的HttpServletRequestWrapper
一、自定义HttpServletRequestWrapper类
1. CustomizeHttpServletRequestWrapper.java
package com.zsx.http;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
@Slf4j
public class CustomizeHttpServletRequestWrapper extends HttpServletRequestWrapper {
private byte[] buffer = {};
/**
* Constructs a request object wrapping the given request.
*
* @param request The request to wrap
* @throws IllegalArgumentException if the request is null
*/
public CustomizeHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
log.info("=========CustomizeHttpServletRequestWrapper.CustomizeHttpServletRequestWrapper()=========");
try {
InputStream is = request.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int read = 0;
while ((read = is.read(buff)) > 0) {
os.write(buff, 0, read);
this.buffer = os.toByteArray();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public ServletInputStream getInputStream() {
log.info("=========CustomizeHttpServletRequestWrapper.getInputStream()=========");
final ByteArrayInputStream is = new ByteArrayInputStream(buffer);
return new ServletInputStream() {
@Override
public int read() {
return is.read();
}
@Override
public boolean isFinished() {
return is.available() == 0;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
//do nothing
}
};
}
@Override
public BufferedReader getReader() {
log.info("=========CustomizeHttpServletRequestWrapper.getReader()=========");
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
}
二、定义过滤器
1. RequestBodyParameterReadingFilter.java
package com.zsx.filter;
import com.zsx.http.CustomizeHttpServletRequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Slf4j
@Configuration
public class RequestBodyParameterReadingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("=========RequestBodyParameterReadingFilter.init()=========");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("=========RequestBodyParameterReadingFilter.doFilter()=========");
if (request instanceof HttpServletRequest) {
String contentType = request.getContentType();
// 如果处理上传文件数据,下面方法执行到chain.doFilter()时会出线异常,所以此处只处理@RequestBody数据
if (!StringUtils.isEmpty(contentType) && contentType.contains(MediaType.APPLICATION_JSON_VALUE)) {
log.info("=========RequestBodyParameterReadingFilter.doFilter().jsonRequest=========");
ServletRequest jsonRequest = new CustomizeHttpServletRequestWrapper((HttpServletRequest) request);
chain.doFilter(jsonRequest, response);
return;
}
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
log.info("=========RequestBodyParameterReadingFilter.destroy()=========");
}
}
三、定义全局异常处理器
1. GlobalExceptionHandler.java
package com.zsx.exception;
import com.zsx.http.CustomizeHttpServletRequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.stream.Collectors;
@Slf4j
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
@SuppressWarnings("NullableProblems")
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, @Nullable Object body,
HttpHeaders headers,
HttpStatus status,
WebRequest request) {
try {
log.info("=========GeneralExceptionHandler.handleExceptionInternal()=========");
HttpServletRequest req = ((ServletWebRequest) request).getRequest();
String bodyParams = req.getReader().lines().collect(Collectors.joining());
log.info("RequestBody Parameters : {} ", bodyParams);
} catch (IOException e) {
e.printStackTrace();
log.error("GeneralExceptionHandler.handleExceptionInternal().IOException : {}", ex.getMessage());
}
return super.handleExceptionInternal(ex, body, headers, status, request);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> exception(Exception ex, HttpServletRequest request) {
log.info("=========GeneralExceptionHandler.exception()=========");
CustomizeHttpServletRequestWrapper wrapper = new CustomizeHttpServletRequestWrapper(request);
BufferedReader reader = wrapper.getReader();
String bodyParams = reader.lines().collect(Collectors.joining());
log.info("RequestBody Parameters : {} ", bodyParams);
log.error("GeneralExceptionHandler.exception() : {}", ex.getMessage());
return handleExceptionInternal(ex, bodyParams, HttpHeaders.EMPTY, HttpStatus.NOT_FOUND, new ServletWebRequest(request));
}
}
四、异常控制器测试类
1. ExceptionTestController.java
package com.zsx.controller;
import com.zsx.entity.User;
import org.springframework.web.bind.annotation.*;
@RestContro