带有点 (.) 的 Spring MVC @PathVariable 被截断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16332092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC @PathVariable with dot (.) is getting truncated
提问by Kanagavelu Sugumar
This is continuation of question Spring MVC @PathVariable getting truncated
这是Spring MVC @PathVariable 被截断的问题的延续
Spring forum states that it has fixed(3.2 version) as part of ContentNegotiationManager. see the below link.
https://jira.springsource.org/browse/SPR-6164
https://jira.springsource.org/browse/SPR-7632
Spring 论坛声明它已修复(3.2 版本)作为 ContentNegotiationManager 的一部分。请参阅以下链接。
https://jira.springsource.org/browse/SPR-6164
https://jira.springsource.org/browse/SPR-7632
In my application requestParameter with .com is truncated.
在我的应用程序中,带有 .com 的 requestParameter 被截断。
Could anyone explain me how to use this new feature? how is it configurable at xml?
谁能解释一下如何使用这个新功能?它如何在 xml 中配置?
Note: spring forum- #1 Spring MVC @PathVariable with dot (.) is getting truncated
回答by Martin Frey
As far as i know this issue appears only for the pathvariable at the end of the requestmapping.
据我所知,这个问题只出现在请求映射结束时的路径变量上。
We were able to solve that by defining the regex addon in the requestmapping.
我们能够通过在请求映射中定义正则表达式插件来解决这个问题。
/somepath/{variable:.+}
回答by bmeurant
Spring considers that anything behind the last dot is a file extension such as .jsonor .xmland trucate it to retrieve your parameter.
Spring 认为最后一个点后面的任何内容都是文件扩展名,例如.jsonor.xml并截断它以检索您的参数。
So if you have /somepath/{variable}:
所以如果你有/somepath/{variable}:
/somepath/param,/somepath/param.json,/somepath/param.xmlor/somepath/param.anythingwill result in a param with valueparam/somepath/param.value.json,/somepath/param.value.xmlor/somepath/param.value.anythingwill result in a param with valueparam.value
/somepath/param,/somepath/param.json,/somepath/param.xmlor/somepath/param.anything将产生一个带值的参数param/somepath/param.value.json,/somepath/param.value.xml否则/somepath/param.value.anything会产生一个带值的参数param.value
if you change your mapping to /somepath/{variable:.+}as suggested, any dot, including the last one will be consider as part of your parameter :
如果您/somepath/{variable:.+}按照建议将映射更改为,任何点,包括最后一个点都将被视为参数的一部分:
/somepath/paramwill result in a param with valueparam/somepath/param.jsonwill result in a param with valueparam.json/somepath/param.xmlwill result in a param with valueparam.xml/somepath/param.anythingwill result in a param with valueparam.anything/somepath/param.value.jsonwill result in a param with valueparam.value.json- ...
/somepath/param将产生一个带值的参数param/somepath/param.json将产生一个带值的参数param.json/somepath/param.xml将产生一个带值的参数param.xml/somepath/param.anything将产生一个带值的参数param.anything/somepath/param.value.json将产生一个带值的参数param.value.json- ...
If you don't care of extension recognition, you can disable it by overriding mvc:annotation-drivenautomagic :
如果你不关心扩展识别,你可以通过覆盖mvc:annotation-drivenautomagic来禁用它:
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="useSuffixPatternMatch" value="false"/>
</bean>
So, again, if you have /somepath/{variable}:
所以,再一次,如果你有/somepath/{variable}:
/somepath/param,/somepath/param.json,/somepath/param.xmlor/somepath/param.anythingwill result in a param with valueparam/somepath/param.value.json,/somepath/param.value.xmlor/somepath/param.value.anythingwill result in a param with valueparam.value
/somepath/param,/somepath/param.json,/somepath/param.xmlor/somepath/param.anything将产生一个带值的参数param/somepath/param.value.json,/somepath/param.value.xml否则/somepath/param.value.anything会产生一个带值的参数param.value
note : the difference from the default config is visible only if you have a mapping like somepath/something.{variable}. see Resthub project issue
注意:与默认配置的差异仅在您有像somepath/something.{variable}. 请参阅Resthub 项目问题
if you want to keep extension management, since Spring 3.2 you can also set the useRegisteredSuffixPatternMatch property of RequestMappingHandlerMapping bean in order to keep suffixPattern recognition activated but limited to registered extension.
如果你想保持扩展管理,从 Spring 3.2 你还可以设置 RequestMappingHandlerMapping bean 的 useRegisteredSuffixPatternMatch 属性,以保持 suffixPattern 识别激活但仅限于注册扩展。
Here you define only json and xml extensions :
在这里你只定义了 json 和 xml 扩展:
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="useRegisteredSuffixPatternMatch" value="true"/>
</bean>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false"/>
<property name="favorParameter" value="true"/>
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
Note that mvc:annotation-driven accepts now a contentNegotiation option to provide a custom bean but the property of RequestMappingHandlerMapping has to be changed to true (default false) (cf. https://jira.springsource.org/browse/SPR-7632).
请注意, mvc:annotation-driven 现在接受 contentNegotiation 选项以提供自定义 bean,但 RequestMappingHandlerMapping 的属性必须更改为 true(默认为 false)(参见https://jira.springsource.org/browse/SPR-7632)。
For that reason, you still have to override the all mvc:annotation-driven configuration. I opened a ticket to Spring to ask for a custom RequestMappingHandlerMapping : https://jira.springsource.org/browse/SPR-11253. Please vote if you are intereted in.
因此,您仍然必须覆盖所有 mvc:annotation-driven 配置。我向 Spring 开了一张票,要求自定义 RequestMappingHandlerMapping :https: //jira.springsource.org/browse/SPR-11253。如果您有兴趣,请投票。
While overriding, be carreful to consider also custom Execution management overriding. Otherwise, all your custom Exception mappings will fail. You will have to reuse messageCoverters with a list bean :
在覆盖时,还要注意考虑自定义执行管理覆盖。否则,您所有的自定义异常映射都将失败。您将不得不使用列表 bean 重用 messageCoverters:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<util:list id="messageConverters">
<bean class="your.custom.message.converter.IfAny"></bean>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"></bean>
</util:list>
<bean name="exceptionHandlerExceptionResolver"
class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
<property name="order" value="0"/>
<property name="messageConverters" ref="messageConverters"/>
</bean>
<bean name="handlerAdapter"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
<property name="validator" ref="validator" />
</bean>
</property>
<property name="messageConverters" ref="messageConverters"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>
I implemented, in the open source project Resthubthat I am part of, a set of tests on these subjects : see https://github.com/resthub/resthub-spring-stack/pull/219/files& https://github.com/resthub/resthub-spring-stack/issues/217
我在我参与的开源项目Resthub中实施了一组关于这些主题的测试:请参阅https://github.com/resthub/resthub-spring-stack/pull/219/files& https:// github.com/resthub/resthub-spring-stack/issues/217
回答by Dave Syer
Update for Spring 4: since 4.0.1 you can use PathMatchConfigurer(via your WebMvcConfigurer), e.g.
Spring 4 更新:从 4.0.1 开始,您可以使用PathMatchConfigurer(通过您的WebMvcConfigurer),例如
@Configuration
protected static class AllResources extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer matcher) {
matcher.setUseRegisteredSuffixPatternMatch(true);
}
}
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
}
In xml, it would be (https://jira.spring.io/browse/SPR-10163):
在 xml 中,它将是(https://jira.spring.io/browse/SPR-10163):
<mvc:annotation-driven>
[...]
<mvc:path-matching registered-suffixes-only="true"/>
</mvc:annotation-driven>
回答by Micha? Rybak
In addition to Martin Frey's answer, this can also be fixed by adding a trailing slash in the RequestMapping value:
除了 Martin Frey 的回答之外,这也可以通过在 RequestMapping 值中添加尾部斜杠来解决:
/path/{variable}/
Keep in mind that this fix does not support maintainability. It now requires all URI's to have a trailing slash - something that may not be apparent to API users / new developers. Because it's likely not all parameters may have a .in them, it may also create intermittent bugs
请记住,此修复程序不支持可维护性。它现在要求所有 URI 都带有斜杠——这对于 API 用户/新开发人员来说可能并不明显。因为可能不是所有参数都包含一个.,所以它也可能会产生间歇性错误
回答by GoutamS
In Spring Boot Rest Controller, I have resolved these by following Steps:
在 Spring Boot Rest Controller 中,我通过以下步骤解决了这些问题:
RestController :
休息控制器:
@GetMapping("/statusByEmail/{email:.+}/")
public String statusByEmail(@PathVariable(value = "email") String email){
//code
}
And From Rest Client:
从休息客户端:
Get http://mywebhook.com/statusByEmail/[email protected]/
回答by Martin ?ejka
adding the ":.+" worked for me, but not until I removed outer curly brackets.
添加“:.+”对我有用,但直到我删除了外部大括号。
value = {"/username/{id:.+}"}didn't work
value = {"/username/{id:.+}" }没有用
value = "/username/{id:.+}" works
value = "/username/{id:.+}" 有效
Hope I helped someone :)
希望我帮助了某人:)
回答by amit dahiya
/somepath/{variable:.+}works in Java requestMappingtag.
/somepath/{variable:.+}在 JavarequestMapping标签中工作。
回答by Bruno Carrier
Here's an approach that relies purely on java configuration:
这是一种完全依赖于 java 配置的方法:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport{
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
handlerMapping.setUseSuffixPatternMatch(false);
handlerMapping.setUseTrailingSlashMatch(false);
return handlerMapping;
}
}
回答by Dapper Dan
In Spring Boot, The Regular expression solve the problem like
在 Spring Boot 中,正则表达式解决了类似的问题
@GetMapping("/path/{param1:.+}")
回答by Marcelo C.
One pretty easy way to work around this issue is to append a trailing slash ...
解决此问题的一种非常简单的方法是在尾部添加一个斜杠...
e.g.:
例如:
use :
用 :
/somepath/filename.jpg/
instead of:
代替:
/somepath/filename.jpg

