Junior — Middle
What annotations in popular libraries are used for handling HTTP requests in your programming language?
sobes.tech AI
Answer from AI
In Java, annotations from the Spring Framework are often used for handling HTTP requests, for example:
@RequestMapping— to specify the path and HTTP method.@GetMapping,@PostMapping,@PutMapping,@DeleteMapping— specialized annotations for respective HTTP methods.@PathVariable— to obtain variables from the URL.@RequestParam— to get request parameters.@RequestBody— to bind the request body to an object.
Example:
@RestController
public class UserController {
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) {
// logic to get user by id
}
@PostMapping("/users")
public User createUser(@RequestBody User user) {
// logic to create a user
}
}