Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring URI Template Patterns with Regular Expressions

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?

like image 236
Asp1de Avatar asked Jan 26 '12 15:01

Asp1de


2 Answers

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) {
like image 137
aweigold Avatar answered Sep 29 '22 07:09

aweigold


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
like image 34
Tomasz Nurkiewicz Avatar answered Sep 29 '22 07:09

Tomasz Nurkiewicz