views:

82

answers:

1

Designing my RESTful API, I would like use following URI

http://[HOST]/[PLANET]/[LAT];[LONG]

e.g.

http://myserver/earth/50.2;29.1

What is the appropiate annotation of a such a method in Spring MVC? Is this the following one ok?

@RequestMapping(value = "/{planet}/{lat};{long}", method = RequestMethod.GET)
public String showInfoAboutCoords(
  @PathVariable final String planet, 
  @PathVariable final String lat,
  @PathVariable final String long, 
  final HttpServletResponse response) {
        // Implementation
}

If this one is ok - what is @MaskFormat("###-##-####") good for?

+1  A: 

Your URI pattern has two problems:

  • Some servlet containers may treat ; as a delimiter and trim the URI (for example Tomcat's bug 30535). So, as a workaround you may use some different character, like ,.
  • By default Spring MVC treats point in URI as an extension separator and trims it too. You can override it by specifying regexp pattern for path variable.

So, you'll have something like

@RequestMapping(value = "/{planet}/{lat:.*},{long:.*}", method = RequestMethod.GET) 

Note that since you disabled Spring's extension handling, you have to enable it manually if you need it (this also requires more restrictive regexp to avoid confusing decimal point with extension separator):

@RequestMapping(value = 
    {"/{planet}/{lat:.*},{long:\\d+\\.\\d+}", 
         "/{planet}/{lat:.*},{long:\\d+\\.\\d+}.*"}, 
    method = RequestMethod.GET)

By @MaskFormat you probably mean an annotation from mvc-showcase (note that it's note a built-in annotation). Along with MaskFormatAnnotationFormatterFactory it demonstrates the new formatting faclilities to convert path variables (i.e. strings) to method arguments. Actually it converts Strings to Strings, so it's used only for validation.

axtavt
Thanks, it works excellent. And yes, I was referring to the annotation from `mvc-showcase`.