1

I'm using Spring MVC and I have a controller mapped to a url lets call it example. I also have a method called show that allows me to view one of my examples based on an id.

@RequestMapping("/example")


@RequestMapping(value = "/{id}", produces = "text/html")
public String show(@PathVariable("id") String id, Model model) {
    //Do some stuff and return a view
}

The problem is that the id is a URI and it has forward slashes. (e.g. test/case/version/sample might be an id so the resulting url is example/test/case/version/sample) so as a result my application gives me an error "Requested resource not found". I can't easily change the format of these ids. It's a list given to me that I have to work with. Is there a way around this? Thanks in Advance.

Bhopewell
  • 57
  • 6
  • May be [this question](http://stackoverflow.com/questions/4904092/with-spring-3-0-can-i-make-an-optional-path-variable) and [this answer](http://stackoverflow.com/a/16732049/2051952) would help? – dmahapatro Jun 21 '13 at 20:48

1 Answers1

1

You can try using Regular expressions on the @PathVariable.

Like this from the Spring Docs:

@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
  public void handle(@PathVariable String version, @PathVariable String extension) {
    // ...
  }
}

You'll just have to think on a regular expression that matches the "example/test/case/version/sample" that is your expression.

See the title: "URI Template Patterns with Regular Expressions" on this page: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html for more information

renanleandrof
  • 6,699
  • 9
  • 45
  • 67