diff --git a/src/main/java/REST/Application.java b/src/main/java/REST/Application.java new file mode 100644 index 0000000000000000000000000000000000000000..bb42c6b94c7a9a70caf898354fe6189b925f61dd --- /dev/null +++ b/src/main/java/REST/Application.java @@ -0,0 +1,14 @@ +package REST; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + System.out.println("Server running on http://localhost:8080"); + } +} + diff --git a/src/main/java/REST/ClientExample.java b/src/main/java/REST/ClientExample.java new file mode 100644 index 0000000000000000000000000000000000000000..509c8002fca6530b7d065306b81d9326171d481f --- /dev/null +++ b/src/main/java/REST/ClientExample.java @@ -0,0 +1,46 @@ +package REST; + +import org.springframework.http.*; +import org.springframework.web.client.RestTemplate; + +public class ClientExample { + + public static void main(String[] args) { + RestTemplate client = new RestTemplate(); + String serverAddress = "http://localhost:8080"; + + // POST EXAMPLE + String postPath = "/users/add"; + User user = new User("John", "Cena"); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity request = new HttpEntity<>(user, headers); + + ResponseEntity postResponse = client.postForEntity(serverAddress + postPath, request, Void.class); + System.out.println("POST Response: " + postResponse.getStatusCode()); + + // GET REQUEST #1 (get all users) + String getPath = "/users"; + ResponseEntity getAllResponse = client.getForEntity(serverAddress + getPath, User[].class); + System.out.println("GET All Response: " + getAllResponse.getStatusCode()); + + User[] users = getAllResponse.getBody(); + if (users != null) { + System.out.println("Users List:"); + for (User u : users) { + System.out.println("Name: " + u.getName() + ", Surname: " + u.getSurname()); + } + } + + // GET REQUEST #2 (get by name) + String getUserPath = "/users/get/john"; + ResponseEntity userResponse = client.getForEntity(serverAddress + getUserPath, User.class); + User u = userResponse.getBody(); + if (u != null) { + System.out.println("Name: " + u.getName() + ", Surname: " + u.getSurname()); + } else { + System.out.println("User not found."); + } + } +} diff --git a/src/main/java/REST/User.java b/src/main/java/REST/User.java new file mode 100644 index 0000000000000000000000000000000000000000..5286e422d3e109794df979ae3d653eee7ae2c35a --- /dev/null +++ b/src/main/java/REST/User.java @@ -0,0 +1,33 @@ +package REST; + + +public class User { + + private String name; + private String surname; + + //Remember to always add an empty constructor + public User() { + } + + public User(String name, String surname) { + this.name = name; + this.surname = surname; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } +} diff --git a/src/main/java/REST/UserService.java b/src/main/java/REST/UserService.java new file mode 100644 index 0000000000000000000000000000000000000000..8521f943ff07ee0a1e6363e7b107a091faaf8d4f --- /dev/null +++ b/src/main/java/REST/UserService.java @@ -0,0 +1,38 @@ +package REST; + +import org.springframework.stereotype.Service; + + +import java.util.ArrayList; +import java.util.List; + +@Service //Spring automatically makes this class a singleton bean +public class UserService { + + private final List usersList = new ArrayList<>(); + + public synchronized List getUsersList() { + return new ArrayList<>(usersList); + } + + public synchronized void setUsersList(List users) { + usersList.clear(); + usersList.addAll(users); + } + + public synchronized void add(User user) { + usersList.add(user); + } + + public synchronized User getByName(String name) { + System.out.println("called"); + for (User user : usersList) { + System.out.println(user.getName()); + System.out.println(name); + if (user.getName() != null && user.getName().equalsIgnoreCase(name)) { + return user; + } + } + return null; // Not found + } +} diff --git a/src/main/java/REST/UsersController.java b/src/main/java/REST/UsersController.java new file mode 100644 index 0000000000000000000000000000000000000000..b83a153ec5e2efe0f7326ba5f2889e2fc57ffba8 --- /dev/null +++ b/src/main/java/REST/UsersController.java @@ -0,0 +1,48 @@ +package REST; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/users") +public class UsersController { + + private final UserService userService; + + @Autowired //tells Spring to automatically resolve and inject the required bean (object) into the class where it's used + public UsersController(UserService userService) { + this.userService = userService; + } + + // Return list of users + @GetMapping + public ResponseEntity> getUsersList() { + return ResponseEntity.ok(userService.getUsersList()); + } + + // Add a new user + @PostMapping("/add") + public ResponseEntity addUser(@RequestBody User user) { + userService.add(user); + return ResponseEntity.ok().build(); + } + + // Get user by name + @GetMapping("/get/{name}") + public ResponseEntity getByName(@PathVariable String name) { + User user = userService.getByName(name); + System.out.println(name); + if (user != null) + return ResponseEntity.ok(user); + else + return ResponseEntity.notFound().build(); + } +} + + + + +