这是问题的延续 Spring MVC @PathVariable被截断

Spring论坛声明它已经固定(3.2版本)作为ContentNegotiationManager的一部分。请看下面的链接。 https://jira.springsource.org/browse/SPR-6164 https://jira.springsource.org/browse/SPR-7632

在我的应用程序中,带有。com的requestParameter被截断了。

谁能告诉我如何使用这个新功能?如何在xml中配置它?

注:春季论坛- #1 Spring MVC @PathVariable带点(.)会被截断


当前回答

最后,我在Spring Docs中找到了解决方案:

要完全禁用文件扩展名,您必须同时设置以下两项: useSuffixPatternMatching(false),参见PathMatchConfigurer favorpatheextension (false),参见ContentNegotiationConfigurer

将此添加到我的WebMvcConfigurerAdapter实现解决了这个问题:

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}

@Override
public void configurePathMatch(PathMatchConfigurer matcher) {
    matcher.setUseSuffixPatternMatch(false);
}

其他回答

/ somepath /{变量:。+}适用于Java requestMapping标签。

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

在xml中,它是(https://jira.spring.io/browse/SPR-10163):

<mvc:annotation-driven>
    [...]
    <mvc:path-matching registered-suffixes-only="true"/>
</mvc:annotation-driven>

据我所知,这个问题只出现在请求映射结束时的路径变量。

我们可以通过在requestmapping中定义regex插件来解决这个问题。

 /somepath/{variable:.+}

对我来说

@GetMapping(path = "/a/{variableName:.+}")

确实工作,但只有当你也编码的“点”在你的请求url为“%2E”,然后它工作。但要求URL都是…虽然有效,但这不是“标准”编码。感觉像是一个bug:|

另一种类似于“后斜杠”的方法是移动带有点“inline”ex的变量:

@GetMapping(path = "/{variableName}/a")

现在所有的点都将被保留,不需要修改。

在Spring Boot Rest Controller中,我通过以下步骤解决了这些问题:

RestController:

@GetMapping("/statusByEmail/{email:.+}/")
public String statusByEmail(@PathVariable(value = "email") String email){
  //code
}

从Rest客户端:

Get http://mywebhook.com/statusByEmail/abc.test@gmail.com/