我有一个要求,即使param名称的情况发生变化,我必须使requestParams正确绑定。注意:我使用的是spring 3.2
例如:http://localhost:8080/sample/home?*
* userName ** = xxx或http://localhost:8080/sample/home?
用户名 = xxx或
http://localhost:8080/sample/home?
usernaMe = xxx应正确映射到我的
@RequestParam值。
@RequestMapping(value = "home", method = RequestMethod.GET)
public goToHome(@RequestParam(value = "userName", required = false) String userName) {
}
所有三个网址都应调用上述方法并正确绑定用户名。 请给我一些关于如何通过实现新的参数处理程序解析器来实现它的建议?覆盖一般实现的spring配置类优于更改所有@RequestParam的代码中的逻辑。
答案 0 :(得分:9)
Spring有LinkedCaseInsensitiveMap
您可以使用它来进行不区分大小写的查找。
实施可能如下所示。
package biz.deinum.web.filter;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
/**
* Wrapper for an {@link HttpServletRequest} to make the lookup of parameters case insensitive. The functionality
* is achieved by using the {@link LinkedCaseInsensitiveMap} from Spring.
*
* @author Marten Deinum
*/
public class CaseInsensitiveRequestFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
filterChain.doFilter(new CaseInsensitiveHttpServletRequestWrapper(request), response);
}
private static class CaseInsensitiveHttpServletRequestWrapper extends HttpServletRequestWrapper {
private final LinkedCaseInsensitiveMap<String[]> params = new LinkedCaseInsensitiveMap<>();
/**
* Constructs a request object wrapping the given request.
*
* @param request
* @throws IllegalArgumentException if the request is null
*/
private CaseInsensitiveHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
params.putAll(request.getParameterMap());
}
@Override
public String getParameter(String name) {
String[] values = getParameterValues(name);
if (values == null || values.length == 0) {
return null;
}
return values[0];
}
@Override
public Map<String, String[]> getParameterMap() {
return Collections.unmodifiableMap(this.params);
}
@Override
public Enumeration<String> getParameterNames() {
return Collections.enumeration(this.params.keySet());
}
@Override
public String[] getParameterValues(String name) {
return (String[])params.get(name);
}
}
}
答案 1 :(得分:1)
您可以编写一个执行此操作的servlet过滤器。但它确实需要一些编码工作。
以下是代码的链接 - http://www.acooke.org/cute/Forcinglow0.html
这样的东西 - 在这个servlet过滤器中将参数转换为小写
public final class LowerCaseParametersFilter implements Filter {
@Override
public void doFilter(final ServletRequest request,
final ServletResponse response,
final FilterChain chain)
throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
LOG.debug("Wrapping request");
chain.doFilter(new LowerCaseRequest((HttpServletRequest) request),
response);
} else {
LOG.warn(format("Not wrapping request: %s", request.getClass()));
chain.doFilter(request, response);
}
}
}
这是xml配置 - 你需要
<bean id="delegatingFilter"
class="org.springframework.web.filter.DelegatingFilterProxy"
p:targetBeanName="lowerParams"/>
<bean id="lowerParams"
class="com.isti.bss.mvc.LowerCaseParametersFilter"/>
我做了一些研究,发现了Case-insensitive query string request paramters
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;
}
答案 2 :(得分:0)
实施自定义过滤器的解决方案。
过滤器:
public final class CaseInsensitiveParametersFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
chain.doFilter(new CustomHttpServletRequestWrapper((HttpServletRequest) request), response);
} else {
chain.doFilter(request, response);
}
}
private static class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
private Map<String, String[]> lowerCaseParams = new HashMap<>();
public CustomHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
Map<String, String[]> originalParams = request.getParameterMap();
for (Map.Entry<String, String[]> entry : originalParams.entrySet()) {
lowerCaseParams.put(entry.getKey().toLowerCase(), entry.getValue());
}
}
@Override
public String getParameter(String name) {
String[] values = getParameterValues(name);
if (values != null && values.length > 0) {
return values[0];
} else {
return null;
}
}
@Override
public Map getParameterMap() {
return Collections.unmodifiableMap(lowerCaseParams);
}
@Override
public Enumeration getParameterNames() {
return Collections.enumeration(lowerCaseParams.keySet());
}
@Override
public String[] getParameterValues(String name) {
return lowerCaseParams.get(name);
}
}
Servlet初始化程序:
public class RestServiceInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Filter[] getServletFilters() {
return new Filter[]{new CaseInsensitiveParametersFilter()};
}
}