[英]Case-insensitive query string request parameters
My goal is that all below URI's should work 我的目标是以下所有URI都应该有效
https://rest/xyz? https://开头休息/ XYZ? sort =name
sort = name
https://rest/xyz? https://开头休息/ XYZ? Sort =name
排序 =名称
https://rest/xyz? https://开头休息/ XYZ? filter =name=value
filter = name = value
https://rest/xyz? https://开头休息/ XYZ? Filter =name=value
Filter = name = value
To achieve this, I have created custom filter that overrides the HttpServletRequest that is passed to the FilterChain. 为此,我创建了自定义过滤器来覆盖传递给FilterChain的HttpServletRequest。 Below is the link for this approach:
以下是此方法的链接:
http://forum.springsource.org/archive/index.php/t-87433.html http://forum.springsource.org/archive/index.php/t-87433.html
My code: 我的代码:
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
}
}
In this code, i have overriden getParameterValues(String name) method and achieved case-insensitivity of request paramters, but not sure if i need to override any other methods. 在这段代码中,我重写了getParameterValues(String name)方法并实现了请求参数的大小写不敏感,但不确定我是否需要覆盖任何其他方法。
my doubts: 我的疑惑:
do i need to override other methods also like getParameter() and getParameterNames()? 我是否需要覆盖其他方法,如getParameter()和getParameterNames()?
what internal implementation is impacted with this? 内部实施受此影响的是什么?
which class i can see the code implementation of getParameter(), getParameterNames() and getParameterValues()? 哪个类我可以看到getParameter(),getParameterNames()和getParameterValues()的代码实现?
First, let me say my peace: I don't think modifying the HttpServletRequestWrapper is the way to go. 首先,让我说出我的平安:我不认为修改HttpServletRequestWrapper是要走的路。 I am not even sure how you would go about using it, as my understanding is it's App Server specific.
我甚至不确定你将如何使用它,因为我的理解是它是特定于App Server的。 As a side note, this article has specifics on how to use the HttpServletRequest to get a case-insensitive query param without rolling your own.
作为旁注, 本文详细介绍了如何使用HttpServletRequest获取不区分大小写的查询参数,而无需自行编译。
But, in the spirit of answering your questions: 但是,本着回答你的问题的精神:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.