gpt4 book ai didi

java - Spring URL匹配不区分大小写,当url中有数字时不映射

转载 作者:行者123 更新时间:2023-12-01 14:09:45 24 4
gpt4 key购买 nike

我试图让我的 URL 不区分大小写,并从网上获得了以下代码。

但是现在当我的网址有数字时,它会给我 Status 400 错误。在此更改之前,它运行良好。像 localhost\toplevel\234\text\2342 这样的 URL 现在无法识别。有人可以帮助解释为什么会发生这种情况以及需要更改哪些内容才能使这些网址正常工作。

     public class CaseInsensitiveAnnotationHandlerMapping  extends AntPathMatcher 
protected boolean doMatch(String pattern,
String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
return super.doMatch(pattern.toLowerCase(),
path.toLowerCase(), fullMatch, uriTemplateVariables);
}

}

我的 Servlet-Context.xml

    <beans:bean class="com.darproject.webUtils.CaseInsensitiveAnnotationHandlerMapping" /> 
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<beans:bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<beans:bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="webBindingInitializer">
<beans:bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<beans:property name="conversionService" ref="conversion-service"></beans:property>
<beans:property name="validator">
<beans:bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<beans:property name="providerClass" value="org.hibernate.validator.HibernateValidator"></beans:property>
</beans:bean>
</beans:property>
</beans:bean>
</beans:property>
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="byteArrayConverter"/>
<beans:ref bean="jaxbConverter"/>
<beans:ref bean="jsonConverter"/>
<beans:bean class="org.springframework.http.converter.StringHttpMessageConverter"></beans:bean>
<beans:bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></beans:bean>
<beans:bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></beans:bean>
<beans:bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></beans:bean>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean name="byteArrayConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></beans:bean>
<beans:bean name="jaxbConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></beans:bean>
<beans:bean name="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></beans:bean>
<beans:bean name="caseInsensitivePathMatcher" class="com.darproject.webUtils.CaseInsensitiveAnnotationHandlerMapping"/>
<beans:bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<beans:property name="pathMatcher" ref="caseInsensitivePathMatcher"></beans:property>
</beans:bean>

现在不起作用的方法。

@RequestMapping(value="/commentswindow/{defectId}/{release}/{defectsRowID}",method=RequestMethod.POST)
public String addUserComments(
@PathVariable("defectsRowID") long defectsRowID,
@PathVariable("defectId") String defectId,
@PathVariable("release") String release,
@ModelAttribute("addComments") UserCommentsModel cmtModel,
BindingResult results,
Model model,
HttpServletRequest request,
HttpServletResponse response){

最佳答案

不区分大小写与路径变量映射冲突。您可以将变量名称更改为全部小写:defectId --> defectid。它应该可以工作。

@RequestMapping(value="/commentswindow/{defectid}/{release}/{defectsrowid}",method=RequestMethod.POST)public String addUserComments(
@PathVariable("defectsrowid") long defectsRowID,
@PathVariable("defectid") String defectId,
@PathVariable("release") String release,
@ModelAttribute("addComments") UserCommentsModel cmtModel,
BindingResult results,
Model model,
HttpServletRequest request,
HttpServletResponse response){

关于java - Spring URL匹配不区分大小写,当url中有数字时不映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18624795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com