Sobes.tech
Junior — Middle

What annotation in Spring Boot is used to define a route with a path parameter?

sobes.tech AI

Answer from AI

In Spring Boot, to define a route with a path parameter, the @PathVariable annotation is used together with the route annotation, such as @GetMapping or @RequestMapping. The path parameter is specified within curly braces in the route string.

Example:

@GetMapping("/users/{id}")
public User getUserById(@PathVariable("id") Long userId) {
    // logic to retrieve user by id
}

Here, {id} is the path parameter, which is linked to the method argument via @PathVariable.