使用“ HttpServletRequestWrapper”时获取空请求正文

时间:2018-10-12 10:35:08

标签: java ajax filter httprequest

使用Postman呼叫终点时获得适当的身体, 但是,当使用ajax从.ftl文件中使用相同的请求主体到达相同的端点时,则获取空的请求主体。

这是我的自定义requesWrapper类。这是我在http://natch3z.blogspot.com/2009/01/read-request-body-in-filter.html

的帮助下实现的
public class RequestWrapper extends HttpServletRequestWrapper {
private final String body;


public RequestWrapper(HttpServletRequest request) throws IOException
{

    super(request);

    //So that other request method behave just like before
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;
    try {
        InputStream inputStream = request.getInputStream();
        if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            char[] charBuffer = new char[128];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                stringBuilder.append(charBuffer, 0, bytesRead);
            }
        } else {
            stringBuilder.append("");
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ex) {
                throw ex;
            }
        }
    }
    //Store request body content in 'body' variable
    body = stringBuilder.toString();
}

@Override
public String getParameter(String name) {
    if (this.params.containsKey(name)) {
        return this.params.get(name)[0];
    }
    return "";
}

@Override
public Map<String, String[]> getParameterMap() {
    return this.params;
}

@Override
public Enumeration<String> getParameterNames() {
    return new Enumerator<>(params.keySet());
}

@Override
public String[] getParameterValues(String name) {
    return params.get(name);
}

/**
 * add a paramtermap to the exiting map
 */
public void addParameterMap(Map<String, String[]> map) {
    this.params.putAll(map);
}


/**
 * add a header with given name and value
 *
 * @param name
 * @param value
 */
public void addHeader(String name, String value) {
    headerMap.put(name, value);
}

@Override
public String getHeader(String name) {
    String headerValue = super.getHeader(name);
    if (headerMap.containsKey(name)) {
        headerValue = headerMap.get(name);
    }
    return headerValue;
}

/**
 * get the Header names
 */
@Override
public Enumeration<String> getHeaderNames() {
    List<String> names = Collections.list(super.getHeaderNames());
    for (String name : headerMap.keySet()) {
        names.add(name);
    }
    return Collections.enumeration(names);
}

@Override
public Enumeration<String> getHeaders(String name) {
    List<String> values = Collections.list(super.getHeaders(name));
    if (headerMap.containsKey(name)) {
        values.add(headerMap.get(name));
    }
    return Collections.enumeration(values);
}

@Override
public BufferedReader getReader() throws IOException {
    return new BufferedReader(new InputStreamReader(this.getInputStream()));
}

//Use this method to read the request body N times
public String getBody() {
    return this.body;
}

@Override
public ServletInputStream getInputStream() throws IOException {
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
    ServletInputStream servletInputStream = new ServletInputStream() {
        public int read() throws IOException {
            return byteArrayInputStream.read();
        }
        //we have to provide body of below method because they are abstract in ServletInputStream class
        @Override
        public boolean isFinished() {
            return false;
        }

        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setReadListener(ReadListener listener) {
        }
    };
    return servletInputStream;
}
}

我正在过滤器级别使用此requestWrapper并多次求和正文。

0 个答案:

没有答案