使请求参数绑定不区分大小写 [英] Making a request parameter binding case insensitive

查看:195
本文介绍了使请求参数绑定不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,即使param名称的情况发生变化,我必须使requestParams正确绑定。注意:我使用的是spring 3.2

I have a requirement where I have to make requestParams to bind properly even if the cases of the param name changes. Note:I am using spring 3.2

例如: http:// localhost:8080 / sample / home?* * userName ** = xxx或 http:// localhost:8080 / sample / home? 用户名 = xxx或
http:// localhost:8080 / sample / home? usernaMe = xxx应该正确映射到我的
@RequestParam值。

For eg: http://localhost:8080/sample/home?**userName**=xxx or http://localhost:8080/sample/home?username=xxx or http://localhost:8080/sample/home?usernaMe=xxx should map properly to my @RequestParam value.

@RequestMapping(value = "home", method = RequestMethod.GET)
public goToHome(@RequestParam(value = "userName", required = false) String userName) {

}

所有这三个urls应该调用上面的方法并正确绑定用户名。
请通过实现新的参数处理程序解析器给我关于如何实现它的建议?一般来说,重写弹簧配置类是优先于更改所有@RequestParam的代码中的逻辑。

All the three urls should call the above method and bind the user name properly. Please give me suggestions on how to implement this by implementing new argument handler resolver? Overriding spring config classes to implement generically is preferred over changing the logic in the code for all @RequestParam.

推荐答案

Spring有一个< a href =http://docs.spring.io/spring-framework/docs/4.1.6.RELEASE/javadoc-api/org/springframework/util/LinkedCaseInsensitiveMap.html =noreferrer> LinkedCaseInsensitiveMap 您可以使用它来执行不区分大小写的查找。

Spring has a LinkedCaseInsensitiveMap Which you could use to do case insensitive lookups.

实现可能如下所示。

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

这篇关于使请求参数绑定不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆