Packet request parameters using HTTPServletRequestWrapper

I have a filter that authenticates / resolves REST calls. These filters need to access the request parameters, so for this I wrote my own HTTPServletRequestWrapper.

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 doFilter filter 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 get a warning below

WARNING: a servlet request in a URI contains the form of parameters in the request body, but the request body was destroyed by the servlet or servlet filter, access to request parameters. Only resource methods using @FormParam will work as expected. Resource methods that consume the request body in other ways will not work as expected.

I am deploying this to Tomcat. Help!

+4
source share
2 answers

I suppose you use jersey for your REST structure?

I think this basically suggests that since Servlet has now built the Request object, Jersey can no longer determine the difference between the form parameters and the query string parameters.

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

This asks the question - is this really causing you a problem or is the warning just bothering you?

+4
source

Right So I suffer from this problem, and I tried to solve it differently, but I did not want to change the settings of web.xml, because if I tested my application with Postman, it worked fine, but when it was integrated with webapp, it fails with the indicated problem ( 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 pointed out , you only need to set the headers as:

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

and voilรก, the warning is gone.

thanks

+2
source

Source: https://habr.com/ru/post/1483694/


All Articles