简体   繁体   中英

Spring global scope optional path variables

i need to create a global scope optional path variable on my website using Spring, so the same controller could handle a requests with URL's like

https://website.com/something

and

https://website.com/something/5

And i managed to do that this way

@RequestMapping("/something")
public class SomethingController {

  @RequestMapping(value = {"", "/{id}) //<-- important line here
  public Model something([...]) {
    return [...]

But let's say that i have 50 controllers in my application and i don't wan't to modify each and every one of them. Is there any simpler way to accomplish what i need? (application-scope or controller class-scope)

You can try to extend this controllers from Base Controller that is annotated with @RequestMapping (not so flexible), or you can try design patterns like Decorator Pattern. https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM