我的目标是所有低于 URI 的都应该工作
https://rest/xyz? 排序=名称
https://rest/xyz? 排序=名称
https://rest/xyz? 过滤器=名称=值
https://rest/xyz? 过滤器=名称=值
为此,我创建了自定义过滤器,它覆盖了传递给 FilterChain 的 HttpServletRequest。以下是此方法的链接:
http://forum.springsource.org/archive/index.php/t-87433.html
我的代码:
import java.io.IOException;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class HttpCustomParamFilter implements Filter
{
private static class HttpServletRequestCustomeWrapper extends HttpServletRequestWrapper
{
private String[] parameterValues;
@Override
public String[] getParameterValues(String name)
{
Map<String, String[]> localParameterMap = super.getParameterMap();
// Handle case insensitivity of http request paramters like start, count, query, sort, filter etc.
if (localParameterMap != null && !localParameterMap.isEmpty())
{
parameterValues = new String[localParameterMap.size()];
for (String key : localParameterMap.keySet())
{
if (name.equalsIgnoreCase(key))
parameterValues = localParameterMap.get(key);
else
parameterValues = null;
}
}
return parameterValues;
}
public HttpServletRequestCustomWrapper(final ServletRequest request)
{
super((HttpServletRequest) request);
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// override the request passed to the FilterChain
chain.doFilter(new HttpServletRequestCustomWrapper(request), response);
}
@Override
public void init(FilterConfig filterConfig)
throws ServletException
{
// TODO Auto-generated method stub
}
@Override
public void destroy()
{
// TODO Auto-generated method stub
}
}
在这段代码中,我重写了 getParameterValues(String name) 方法并实现了请求参数的不区分大小写,但不确定是否需要重写任何其他方法。
我的疑问:
我是否需要覆盖其他方法,例如 getParameter() 和 getParameterNames()?
这会影响哪些内部实施?
哪个类我可以看到getParameter()、getParameterNames()和getParameterValues()的代码实现?