//UserService package boundary; import entity.User; import java.util.LinkedList; import java.util.List; public class UserService { private List users; public UserService() { users=new LinkedList(); } public List getUsers() { return users; } public User getUserByIndex(int userId) { for(User user : users) { if(user.getId() == userId) return user; } return null; } public boolean deleteUserById(int userId) { int userIndex = -1; for(User user : users) { if(user.getId() == userId) { userIndex = users.indexOf(user); break; } } if(userIndex > -1) { users.remove(userIndex); } else { return false; } return true; } public void addUser(User user) { users.add(user); } } //UserController package control; import boundary.UserService; import entity.User; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import java.util.List; @Path("/registration") @ApplicationPath("rest") //http://localhost:18080/rest/registration/user public class UserController { private UserService userService; @GET @Path("/user/{index}") @Produces({MediaType.APPLICATION_JSON}) public User getObject(@PathParam("index") Integer userId) { return userService.getUserByIndex(userId); } @GET @Path("/user") @Produces({MediaType.APPLICATION_JSON}) public List getAll() { return userService.getUsers(); } @DELETE @Path("/user/{index}") @Produces({MediaType.APPLICATION_JSON}) public boolean deleteObject(@PathParam("index") Integer userId) { return userService.deleteUserById(userId); } @POST @Path("/user/{index}") @Produces({MediaType.APPLICATION_JSON}) public User createObject(@PathParam("index") Integer userId){ User user=new User.Builder() .id(userId) .firstName("Jan") .lastName("Kowalski") .login("jkowal") .bornDate("22.06.1996") .password("haslo") .email("jan.kowalski@gmail.com") .build(); userService.addUser(user); return user; } @PUT @Path("/user/{index}") @Produces({MediaType.APPLICATION_JSON}) public User updateObject(@PathParam("index") Integer userId) { userService.getUserByIndex(userId).setEmail("test@test.pl"); userService.getUserByIndex(userId).setPassword("sssss"); return userService.getUserByIndex(userId); } } //User package entity; public class User { private Integer id; private String firstName; private String lastName; private String login; private String email; private String bornDate; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getBornDate() { return bornDate; } public void setBornDate(String bornDate) { this.bornDate = bornDate; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public static final class Builder { private Integer id; private String firstName; private String lastName; private String login; private String email; private String bornDate; private String password; public Builder id(Integer id) { this.id = id; return this; } public Builder firstName(String firstName) { this.firstName = firstName; return this; } public Builder login(String login) { this.login = login; return this; } public Builder lastName(String lastName) { this.lastName = lastName; return this; } public Builder email(String email) { this.email = email; return this; } public Builder bornDate(String bornDate) { this.bornDate = bornDate; return this; } public Builder password(String password) { this.password = password; return this; } public User build() { if (email.isEmpty()) { throw new IllegalStateException("E-mail cannot be empty"); } if (password.isEmpty()) { throw new IllegalStateException("Password cannot be empty"); } User user = new User(); user.id = this.id; user.firstName = this.firstName; user.lastName = this.lastName; user.login = this.login; user.bornDate = this.bornDate; user.password = this.password; user.email=this.email; return user; } } }