Spring REST Flashcards

(7 cards)

1
Q

Explain in your own words the principles to which a RESTful Web Service must adhere.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

@RestController

A
  • Marks a class as a REST API controller. Combines @Controller and @ResponseBody — Methods return data (JSON) directly, not view templates

@RestController
public class EmployeeController{
// All methods return JSON automatically
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

@GetMapping

A
  • Handles HTTP GET Requests. Used for retrieving resources.

@GetMapping(“/employees”)
public List<Employee> getAllEmployees(){
return employeeService.findAll();
}</Employee>

@GetMapping(“/employees/{id}”)
public Employee getEmployee(@PathVariable Long id){
return employeeService.findById(id);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

@PostMapping

A

@PostMapping

  • Handles HTTP POST requests. Used for creating new resources

@PostMapping(“/employees”)
public Employee createEmployee(@RequestBody Employee employee){
return employeeService.save(employee);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

@RequestMapping

A
  • Maps HTTP requests to controller classes or methods; can specify path and HTTP method.

```java
@RequestMapping(“/employees”)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

@PathVariable

A
  • Binds a URL path parameter to a method argument

@PathVariable Long id

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

@RequestBody

A

Automatically converts the HTTP request body (JSON) into a java object.

@RequestBody Employee employee
How well did you know this?
1
Not at all
2
3
4
5
Perfectly