Commit da26ca18 authored by Michele Fiori's avatar Michele Fiori

Added missing REST files

parent 499f59e6
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");
}
}
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<User> request = new HttpEntity<>(user, headers);
ResponseEntity<Void> 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<User[]> 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<User> 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.");
}
}
}
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;
}
}
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<User> usersList = new ArrayList<>();
public synchronized List<User> getUsersList() {
return new ArrayList<>(usersList);
}
public synchronized void setUsersList(List<User> 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
}
}
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<List<User>> getUsersList() {
return ResponseEntity.ok(userService.getUsersList());
}
// Add a new user
@PostMapping("/add")
public ResponseEntity<Void> addUser(@RequestBody User user) {
userService.add(user);
return ResponseEntity.ok().build();
}
// Get user by name
@GetMapping("/get/{name}")
public ResponseEntity<User> 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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment