Consider the following example:
@Controller
@RequestMapping({"/home"})
public class Home {
@RequestMapping(value = { "", "/" })
public String index() {
return "home";
}
@RequestMapping(value = { "-of-{id}" })
public String of(@PathVariable("id") String id) {
System.out.println(id);
return "home";
}
}
index() is mapped to '/home' and '/home/' perfectly; but of(id) is mapped to '/home/-of-{id}' when I want it to be mapped to '/home-of-{id}'.
Spring add a slash between '/home' and '-of-{id}' automatically, but I want to eliminate it, any suggestion?
When you define a class-level mapping, Spring treats the method-level mappings as additional segments, not as string concatenations. So you can't get the
/home-of-{id}
using the approach you're attempting.Try getting rid of the class-level
@RequestMapping
, and replace the existing method-level mappings with fully-explicit paths. Put@RequestMapping("/home-of-{id}")
onof()
.If you are going to have lots of
/home/
paths that you want to support (and thus you want to keep the class-level definition in place), just move the/home-of-{id}
to a separate controller and you can do that.