-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRESTAPIExample.java
More file actions
69 lines (55 loc) · 1.68 KB
/
Copy pathRESTAPIExample.java
File metadata and controls
69 lines (55 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Day 36 - REST API: Simulated Controller
*/
import java.util.*;
class User {
public int id;
public String name;
public String email;
public User(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
}
class UserAPIController {
private List<User> users = new ArrayList<>();
public UserAPIController() {
users.add(new User(1, "John", "john@example.com"));
users.add(new User(2, "Jane", "jane@example.com"));
}
// GET /users
public List<User> getAllUsers() {
return users;
}
// GET /users/{id}
public User getUserById(int id) {
return users.stream()
.filter(u -> u.id == id)
.findFirst()
.orElse(null);
}
// POST /users
public User createUser(String name, String email) {
int newId = users.size() + 1;
User user = new User(newId, name, email);
users.add(user);
return user;
}
}
public class RESTAPIExample {
public static void main(String[] args) {
System.out.println("=== REST API Simulation ===\n");
UserAPIController controller = new UserAPIController();
System.out.println("GET /users:");
controller.getAllUsers().forEach(u ->
System.out.println(" " + u.id + ": " + u.name)
);
System.out.println("\nGET /users/1:");
User user = controller.getUserById(1);
System.out.println(" " + user.name);
System.out.println("\nPOST /users:");
User newUser = controller.createUser("Bob", "bob@example.com");
System.out.println(" Created: " + newUser.name);
}
}