java.lang.IllegalStateException: getReader() 已被调用用于此请求

2022-09-01 02:06:15

我想将日志记录添加到我的Servlet中,所以我创建了过滤器,它应该显示请求并转到Servlet。但不幸的是,我提出了例外:

java.lang.IllegalStateException: getReader() has already been called for this request
    at org.apache.catalina.connector.Request.getInputStream(Request.java:948)
    at org.apache.catalina.connector.RequestFacade.getInputStream(RequestFacade.java:338)
    at com.noelios.restlet.ext.servlet.ServletCall.getRequestEntityStream(ServletCall.java:190)

因此,为了解决这个问题,我已经找到了Wrappler的解决方案,但它不起作用。我还可以在代码中使用/更改哪些内容?有什么想法吗?

[MyHttpServletRequestWrapper]

public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper
{
    public MyHttpServletRequestWrapper(HttpServletRequest request)
    {
        super(request);
    }

    private String getBodyAsString()
    {
        StringBuffer buff = new StringBuffer();
        buff.append(" BODY_DATA START [ ");
        char[] charArr = new char[getContentLength()];
        try
        {
            BufferedReader reader = new BufferedReader(getReader());
            reader.read(charArr, 0, charArr.length);
            reader.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        buff.append(charArr);
        buff.append(" ] BODY_DATA END ");
        return buff.toString();
    }

    public String toString()
    {
        return getBodyAsString();
    }
}

[我的过滤器]

public class MyFilterimplements Filter
{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        final HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        final HttpServletRequestWrapper requestWrapper = new MyHttpServletRequestWrapper(httpServletRequest);
        final String requestBody = requestWrapper.toString();

        chain.doFilter(request, response);
    }
}

答案 1

看起来 restlet 框架已经调用了 Request 对象,而 Request 对象又调用了 ,因此调用请求会抛出 。getReader() 和 getInputStream() 的 Servlet API 文档说:getRequestEntityStream()getInputStream()getReader()IllegalStateException

 public java.io.BufferedReader getReader()
    ...
    ...
Throws:
    java.lang.IllegalStateException - if getInputStream() method has been called on this request

 public ServletInputStream getInputStream()
    ...
    ...
    Throws:
    java.lang.IllegalStateException - if the getReader() method has already been called for this request

从文档中可以看出,我们似乎无法在 Request 对象上同时调用 getReader() 和 getInputStream()。我建议你使用而不是在你的包装器中。getInputStream()getReader()


答案 2

使用 ContentCachingRequestWrapper 类。将 HttpServletRequest 包装在它中将解决问题

示例:如果你想转换你的“HttpServletRequest servletRequest”,你可以做一些事情,比如

import org.springframework.web.util.ContentCachingRequestWrapper;

ContentCachingRequestWrapper request = new ContentCachingRequestWrapper(servletRequest);

希望它有帮助!!!


推荐