Explain in your own words the principles to which a RESTful Web Service must adhere.
Client-Server separation: Frontend and backend are independent; either can change without affecting the other
Statelessness: Each request contains all information needed. Server stores no client context between requests
Uniform Interface: Standard HTTP methods with consistent meanings: GET(read), POST(create), PUT(update), DELETE(remove).
Resource-Based: Everything is a resource identified by a URL. Use nouns (/employees) rather than verbs (/getEmployees)
Representation: Clients receive representations (JSON, XML) of resources, not the resources themselves
Layered System: Client doesn’t know if its talking to the server directly or through intermediaries (proxies, load balancers)
Cacheability: Responses indicate if they can be cached to improve performance.
@RestController
@RestController
public class EmployeeController{
// All methods return JSON automatically
}
@GetMapping
@GetMapping(“/employees”)
public List<Employee> getAllEmployees(){
return employeeService.findAll();
}</Employee>
@GetMapping(“/employees/{id}”)
public Employee getEmployee(@PathVariable Long id){
return employeeService.findById(id);
}
@PostMapping
@PostMapping
@PostMapping(“/employees”)
public Employee createEmployee(@RequestBody Employee employee){
return employeeService.save(employee);
}
@RequestMapping
```java
@RequestMapping(“/employees”)
~~~
@PathVariable
@PathVariable Long id
@RequestBody
Automatically converts the HTTP request body (JSON) into a java object.
@RequestBody Employee employee