Wrapping request parameters using HTTPServletReque

2020-07-17 15:58发布

问题:

I have a filter that authenticates/authorizes REST calls. This filters needs to access the request parameters so I have written a custom HTTPServletRequestWrapper for this.

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class WrapperRequest extends HttpServletRequestWrapper {
    private Map<String, String[]> requestParams = null;

    public WrapperRequest(final ServletRequest request) {
        super((HttpServletRequest) request);

    }

    @Override
    public String getParameter(final String name) {
        if (getParameterMap().get(name) != null) {
            return getParameterMap().get(name)[0];
        } else {
            getParameterMap().get(name)[0] = super.getParameter(name);
            requestParams.put(name, getParameterMap().get(name));
            return requestParams.get(name)[0];
        }

    }

    @Override
    public Map<String, String[]> getParameterMap() {
        if (requestParams == null) {
            requestParams = new HashMap<String, String[]>();
            requestParams.putAll(super.getParameterMap());
        }
        return Collections.unmodifiableMap(requestParams);
    }

    @Override
    public Enumeration<String> getParameterNames() {
        return Collections.enumeration(getParameterMap().keySet());
    }

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

In my filter doFilter method:

public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {


    final WrapperRequest wrappedRequest = new WrapperRequest(request);
    Map<String, String[]> paramMap = wrappedRequest.getParameterMap();
    chain.doFilter(wrappedRequest, response);

But I am getting the below warning

WARNING: A servlet request, to the URI , contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.

I am deploying this in Tomcat. Help!

回答1:

I take it you are using Jersey for your REST framework?

I think this is basically saying that since the Servlet has now constructed the Request object, Jersey now cant tell the difference between form parameters and query string params.

See this for some details: https://issues.apache.org/jira/browse/STANBOL-437

This begs the question - is this actually causing you an issue or are you just worried about the warning message?



回答2:

Right. So I've been suffering this issue, and I've been trying to solve it on different ways, but I did't want to change my web.xml settings, just because if I was testing my application with Postman it worked perfect, but when it was being integrated with the webapp it fails with the mentioned issue (A servlet request to the URI {MY_URI} contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.)

So as @clijk mentioned, you only have to set your headers as:

"Content-Type": "application/json" 
"charset": "UTF-8" 

and voilá, the warning it's gone.

Thanks