有什么方法可以让 Spring 中的 PathVariable 名称不区分大小写吗?

2024-04-26

我有一个 URIhttps://localhost/Message/message?id=10它会给出 id=10 的消息详细信息。

当我输入如下 URI 时,我想得到相同的响应(这里路径变量有不同的大小写)

 https://localhost/Message/message?id=10 
 https://localhost/Message/Message?ID=10 
 https://localhost/Message/mEssage?Id=10 
 https://localhost/Message/MESSAGE?iD=10 

对于 URI/PathVariable(消息)名称: 春季 4.2+ https://jira.spring.io/browse/SPR-13286支持配置不区分大小写的路径匹配。 您可以按如下方式配置:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }
}

For the @RequestParam/请求参数(ID)部分:

您必须手动执行此操作 - Spring Boot 对此不提供开箱即用的支持。基本概念是您必须实现一个自定义 servlet 过滤器,它将参数标准化HttpServletRequest- 例如您可以申请所有这些String.toLowerCase()在将它们传递给您之前@RestController,其中所有请求参数绑定都定义为小写值。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有什么方法可以让 Spring 中的 PathVariable 名称不区分大小写吗? 的相关文章

随机推荐