Spring Mvc Url和参数名称忽略大小写

本文详细介绍了如何在SpringMVC中配置URL路径和请求参数的大小写敏感性,提供了通过配置类和自定义过滤器实现大小写不敏感的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发过程中Spring Mvc 默认 Url和参数名称都是区分大小写的。

 

URL忽略大小写:

复制代码

@Configuration
public class SpringWebConfig extends WebMvcConfigurationSupport {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher pathMatcher = new AntPathMatcher();
        pathMatcher.setCaseSensitive(false);
        configurer.setPathMatcher(pathMatcher);
    }

}

复制代码

 

或者

复制代码

@Configuration
@ComponentScan(value = "com.sino.webapi.web", useDefaultFilters = false, includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = { Controller.class }) })
public class SpringWebConfig extends WebMvcConfigurationSupport{
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping m = new RequestMappingHandlerMapping();
        AntPathMatcher pathMatcher = new AntPathMatcher();
        pathMatcher.setCaseSensitive(false);
        m.setPathMatcher(pathMatcher);
        return m;
    }
}

复制代码

 

参数名忽略大小写:

复制代码

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.filter.OncePerRequestFilter;

public class CaseInsensitiveRequestParameterNameFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        filterChain.doFilter(new CaseInsensitiveParameterNameHttpServletRequest(request), response);
    }

    public static class CaseInsensitiveParameterNameHttpServletRequest extends HttpServletRequestWrapper {
        private final LinkedCaseInsensitiveMap<String[]> map = new LinkedCaseInsensitiveMap<>();

        @SuppressWarnings("unchecked")
        public CaseInsensitiveParameterNameHttpServletRequest(HttpServletRequest request) {
            super(request);
            map.putAll(request.getParameterMap());
        }

        @Override
        public String getParameter(String name) {

            String[] array = this.map.get(name);
            if (array != null && array.length > 0)
                return array[0];
            return null;
        }

        @Override
        public Map<String, String[]> getParameterMap() {
            return Collections.unmodifiableMap(this.map);
        }

        @Override
        public Enumeration<String> getParameterNames() {
            return Collections.enumeration(this.map.keySet());
        }

        @Override
        public String[] getParameterValues(String name) {
            return this.map.get(name);
        }

    }

}

复制代码

Bean:

复制代码

<!--输入参数忽略大小写-->
    <bean id="caseInsensitiveRequestFilterProxy" class="org.springframework.web.filter.DelegatingFilterProxy">
        <property name="targetBeanName" value="caseInsensitiveRequestFilter"/>
    </bean>
    <bean id="caseInsensitiveRequestFilter"
          class="com.hantianwei.util.CaseInsensitiveRequestParameterNameFilter">
    </bean>

复制代码

添加Filter:

复制代码

<filter>
    <filter-name>caseInsensitiveRequestFilterProxy</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>caseInsensitiveRequestFilterProxy</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

复制代码

### 谷粒商城 P64 页面大小写问题解决方案 对于谷粒商城项目中的页面路径大小写敏感问题,可以采取多种方式来处理。通常情况下,在开发环境中配置服务器或者应用框架本身能够有效应对这一挑战。 #### 1. Nginx 配置忽略大小写 如果前端请求通过Nginx转发到后端服务,则可以在Nginx配置文件中加入如下指令: ```nginx location ~* ^/your/path { try_files $uri $uri/ /index.html; } ``` 此配置使得URL匹配时不区分大小写[^1]。 #### 2. Spring Boot 应用层处理 另一种方法是在Spring Boot应用程序内部解决问题。可以通过自定义`WebMvcConfigurer`接口实现类的方式修改默认行为: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setUrlDecode(true); urlPathHelper.setRemoveSemicolonContent(false); AntPathMatcher antPathMatcher = new AntPathMatcher(); antPathMatcher.setCaseSensitive(false); // 关键点:设置不区分大小写 configurer.setUrlPathHelper(urlPathHelper).setPathMatcher(antPathMatcher); } } ``` 上述代码片段展示了如何让Spring MVC路由解析器不再考虑路径中的字母大小写差异[^3]。 此外,为了确保整个系统的稳定性一致性,建议统一规定前后端交互时使用的资源定位符(URL)遵循某种命名约定,比如全部采用小写字母形式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值