具有正则表达式的Spring URI模板模式

时间:2012-01-26 15:24:51

标签: java spring spring-mvc

嘿有人知道怎么能用@RequestMapping匹配这个URI“http:// localhost:8080 / test / user / 127.0.0.1:8002:8”。

我尝试编写此代码:

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET,headers="Accept=application/xml" )
public void test(@PathVariable("id") String id) {
System.out.println(id);
return null;
}

但问题是当我打印id时,值为:127.0.0 也许有些事情会发生什么?

2 个答案:

答案 0 :(得分:12)

请参阅SpEL文档: http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/expressions.html

你会想做这样的事情:

@RequestMapping(value = "/user/{id:.*}", method = RequestMethod.GET,headers="Accept=application/xml" )
public void test(@PathVariable("id") String id) {

答案 1 :(得分:5)

如果你在Spring MVC中使用@Configuration - 样式,那么这就可以了:

@Configuration
public class Api extends WebMvcConfigurationSupport {

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping();
        mapping.setUseSuffixPatternMatch(false);
        return mapping;
    }

}

如您所见,您必须在RequestMappingHandlerMapping中停用useSuffixPatternMatch

另见: