Senior
What is a RESTful Web Service? Key principles.
sobes.tech AI
Answer from AI
RESTful Web Service is a web service that implements the REST (Representational State Transfer) architectural style. It uses standard HTTP methods (GET, POST, PUT, DELETE) to interact with resources identified by URIs.
Key principles of RESTful Web Service:
- Resource identification: each resource has a unique URI.
- Use of standard HTTP methods: operations on resources are performed via GET (retrieve), POST (create), PUT (update), DELETE (delete).
- Statelessness: each client request to the server must contain all the information needed; the server does not store state between requests.
- Caching: responses can be cached to improve performance.
- Uniform interface: standardized methods for interacting with resources.
- Layered system: architecture can consist of multiple layers (proxies, gateways, etc.).
Example of a simple RESTful API in Java using Spring Boot:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
// logic to get user
}
@PostMapping
public User createUser(@RequestBody User user) {
// logic to create user
}
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User user) {
// logic to update user
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
// logic to delete user
}
}