Spring URI Template Patterns with Regular Expressions

穿精又带淫゛_ 提交于 2020-01-10 19:43:14

问题


Hey someone know how can match this URI "http://localhost:8080/test/user/127.0.0.1:8002:8" with @RequestMapping.

I try to write this code:

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

but the problem is when i print the id the value is: 127.0.0. Maybe something is wrong?


回答1:


See the SpEL documentation: http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/expressions.html

You will want to do something like this:

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



回答2:


If you are using @Configuration-style with Spring MVC, this will do the trick:

@Configuration
public class Api extends WebMvcConfigurationSupport {

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

}

As you can see you must disable useSuffixPatternMatch in RequestMappingHandlerMapping.

See also:

  • Configuring RequestMappingHandlerMapping when using mvc:annotation-driven


来源:https://stackoverflow.com/questions/9020444/spring-uri-template-patterns-with-regular-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!