gpt4 book ai didi

java - 使用 HTTPServletRequestWrapper 包装请求参数

转载 作者:搜寻专家 更新时间:2023-11-01 02:12:06 25 4
gpt4 key购买 nike

我有一个过滤器可以验证/授权 REST 调用。此过滤器需要访问请求参数,因此我为此编写了自定义 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);
}
}

在我的过滤器 doFilter 方法中:

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);

但我收到以下警告

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.

我正在 Tomcat 中部署它。帮助!

最佳答案

我认为您正在将 Jersey 用于 REST 框架?

我认为这基本上是说,由于 Servlet 现在已经构造了 Request 对象,因此 Jersey 现在无法区分表单参数和查询字符串参数之间的区别。

有关详细信息,请参阅:https://issues.apache.org/jira/browse/STANBOL-437

这引出了一个问题 - 这实际上会给您带来问题还是您只是担心警告消息?

关于java - 使用 HTTPServletRequestWrapper 包装请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16848180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com