简体   繁体   中英

Can I use path variable in spring controller class?

Can I use path variable for spring controller class?

I know that we can use path variables in controller's methods. In the same fashion can we use it for entire class?
Eg:

@Controller
@RequestMapping(value = "{version}/test")
class TestController {

}

Can we use like this? If yes how do we read {version} variable? Actually i need this kind of approach, based on the version i'll respond. If the above approach is not possible can you please suggest me a design to solve this problem?

Yes you can. Just declare it as @PathVariable in your methods if you want to access it there.

@Controller
@RequestMapping(value = "{version}/test")
class TestController {

    @RequestMapping(value="/something")
    public ModelAndView doSomething(@PathVariable String version) {
       // do something here with the version
    }

}

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