我在拦截器中读取请求正文存在问题。 getReader()
和getInputStream()
都引起问题。
我的拦截器:
public class MyInterceptor extends HandlerInterceptorAdapter {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestBody = httpRequest.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
//or
// String requestBody = new BufferedReader(new InputStreamReader(httpRequest.getInputStream()))
// .lines().collect(Collectors.joining("\n"));
//some logic...
return true;
}
这两种方法都失败了,因为spring可能在内部某个地方使用了这种资源。 首先导致 java.lang.IllegalStateException:此请求已调用getReader(),而其他必需的请求正文丢失:org.springframework.http.ResponseEntity ...
我尝试了一些包装方法,但没有任何效果。我认为这是因为我无法像过滤器一样传递包装器(我不想使用过滤器,因为我有常见的异常管理器(@ControllerAdvice)。
这是一个已知问题吗?有什么解决方法吗?
答案 0 :(得分:1)
最后我已经弄清楚了,所以我将在这里为其他人提供一些简单但有用的建议。 我使用了请求包装器,但为了使其正常工作,我在执行拦截器之前,以最高顺序添加了一个过滤器,以将每个请求包装到包装器中。现在它运作良好;) 这是最重要的代码-用于将每个请求包装到多读取包装器中的过滤器(拦截器的外观与上面几乎相同,包装器不是我发明的,而是在堆栈上找到的,我发现它是最清晰易读的):
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalWrapFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
MultiReadRequest wrapper = new MultiReadRequest((HttpServletRequest) request);
chain.doFilter(wrapper, response);
}
@Override
public void destroy() {
}
class MultiReadRequest extends HttpServletRequestWrapper {
private String requestBody;
public MultiReadRequest(HttpServletRequest request) {
super(request);
try {
requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody.getBytes());
return new ServletInputStream() {
@Override
public boolean isFinished() {
return byteArrayInputStream.available() == 0;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() throws IOException {
return byteArrayInputStream.read();
}
};
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream(), Charset.forName("UTF-8")));
}
}
}