├── src └── main │ └── java │ ├── controller │ ├── Autowired.java │ ├── RequestBody.java │ └── Server.java │ ├── customer │ └── ClienteHTTP.java │ ├── Service │ ├── JPAUsers.java │ └── JPAUsersImpl.java │ ├── model │ └── User.java │ └── org │ └── narvasoft │ └── Main.java ├── .idea ├── codeStyles │ ├── codeStyleConfig.xml │ └── Project.xml ├── vcs.xml ├── .gitignore ├── jpa-buddy.xml ├── encodings.xml └── misc.xml ├── README.md ├── .gitignore └── pom.xml /src/main/java/controller/Autowired.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | public @interface Autowired { 4 | boolean required() default true; 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/controller/RequestBody.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | public @interface RequestBody { 4 | boolean required() default true; 5 | } 6 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SIMULACIÓN DE APIREST EN JAVA 2 | 3 | Este proyecto es una simulación de una API REST de código puro en Java. 4 | No usa ningún FrameWOrk, pero simula una app Web corriendo en SpringBoot 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /.idea/jpa-buddy.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /src/main/java/customer/ClienteHTTP.java: -------------------------------------------------------------------------------- 1 | package customer; 2 | 3 | public class ClienteHTTP { 4 | private String url; 5 | private String headers; 6 | private String method; 7 | private String body; 8 | 9 | } 10 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/Service/JPAUsers.java: -------------------------------------------------------------------------------- 1 | package Service; 2 | 3 | import model.User; 4 | 5 | import java.util.List; 6 | 7 | public interface JPAUsers { 8 | /*Interfaz que contendrá los métodos abtractos(CRUD) 9 | *que se implementarán en la clase JPAUsersImpl 10 | */ 11 | 12 | public void create(String body); 13 | public List readAll(); 14 | public void updateById(String body, int id); 15 | public void deleteById(int id); 16 | public void findAll(); 17 | public void findById(int id); 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea/modules.xml 8 | .idea/jarRepositories.xml 9 | .idea/compiler.xml 10 | .idea/libraries/ 11 | *.iws 12 | *.iml 13 | *.ipr 14 | 15 | ### Eclipse ### 16 | .apt_generated 17 | .classpath 18 | .factorypath 19 | .project 20 | .settings 21 | .springBeans 22 | .sts4-cache 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | build/ 31 | !**/src/main/**/build/ 32 | !**/src/test/**/build/ 33 | 34 | ### VS Code ### 35 | .vscode/ 36 | 37 | ### Mac OS ### 38 | .DS_Store -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.narvasoft 8 | myapi 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 19 13 | 19 14 | UTF-8 15 | 16 | 17 | 18 | com.stabletechs 19 | autowire_2.11 20 | 0.2.6 21 | 22 | 23 | com.stabletechs 24 | autowire_2.11 25 | 0.2.6 26 | 27 | 28 | com.stabletechs 29 | autowire_2.11 30 | 0.2.6 31 | 32 | 33 | org.jetbrains 34 | annotations 35 | RELEASE 36 | compile 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/main/java/model/User.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | public class User { 6 | private static final AtomicInteger count = new AtomicInteger(0); 7 | private int id=0; 8 | private String names; 9 | private String email; 10 | private String phone; 11 | 12 | public User(int id,String names, String email, String phone) { 13 | this.id = count.incrementAndGet(); 14 | //this.setId(id++);//sino se le asigna el id, el id siempre será el mismo 15 | //User.id=id;//incrementamos el id automaticamente cada vez que se cree un nuevo usuario 16 | this.names = names; 17 | this.email = email; 18 | this.phone = phone; 19 | } 20 | 21 | 22 | public int getId() { 23 | return id; 24 | } 25 | 26 | public void setId(int id) { 27 | this.id = id; 28 | } 29 | 30 | public String getNames() { 31 | return names; 32 | } 33 | 34 | public void setNames(String names) { 35 | this.names = names; 36 | } 37 | 38 | public String getEmail() { 39 | return email; 40 | } 41 | 42 | public void setEmail(String email) { 43 | this.email = email; 44 | } 45 | 46 | public String getPhone() { 47 | return phone; 48 | } 49 | 50 | public void setPhone(String phone) { 51 | this.phone = phone; 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "User{" + 57 | "id=" + id + 58 | ", names='" + names + '\'' + 59 | ", email='" + email + '\'' + 60 | ", phone='" + phone + '\'' + 61 | '}'; 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/org/narvasoft/Main.java: -------------------------------------------------------------------------------- 1 | package org.narvasoft; 2 | 3 | import controller.Server; 4 | import model.User; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Server request = new Server("POST", "http://localhost:8080/api/users", "Content-Type: application/json", "Yeshúa,narva@gmail.com,123456789"); 9 | 10 | System.out.println(request);//respuesta del servidor 11 | System.out.println("---------------------------------------------------"); 12 | request = new Server("POST", "http://localhost:8080/api/users", "Content-Type: application/json", "Sophia,gug@gmail.com,3021564"); 13 | System.out.println(request);//respuesta del servidor 14 | System.out.println("---------------------------------------------------"); 15 | 16 | request = new Server("POST", "http://localhost:8080/api/users", "Content-Type: application/json", "Sarah,sarah@gmail.com,303330"); 17 | System.out.println(request);//respuesta del servidor 18 | System.out.println("---------------------------------------------------"); 19 | 20 | 21 | request = new Server("GET", "http://localhost:8080/api/users", "Content-Type: application/json", ""); 22 | System.out.println(request);//respuesta del servidor 23 | 24 | System.out.println("---------------------------------------------------"); 25 | request = new Server("PUT", "http://localhost:8080/api/users", "Content-Type: application/json", "Sophia,gug@gmail.com,5555", 2); 26 | System.out.println(request);//respuesta del servidor 27 | System.out.println("---------------------------------------------------"); 28 | 29 | request = new Server("DELETE", "http://localhost:8080/api/users", "Content-Type: application/json", "",2); 30 | 31 | System.out.println("---------------------------------------------------"); 32 | request = new Server("GET", "http://localhost:8080/api/users", "Content-Type: application/json", "",1); 33 | } 34 | } -------------------------------------------------------------------------------- /src/main/java/Service/JPAUsersImpl.java: -------------------------------------------------------------------------------- 1 | package Service; 2 | 3 | import model.User; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | 9 | public class JPAUsersImpl implements JPAUsers { 10 | private static List users = new ArrayList(); 11 | public static User user; 12 | 13 | @Override 14 | public void create(String body) { 15 | 16 | String[] data = body.split(","); 17 | 18 | user = new User(0, data[0], data[1], data[2]); 19 | String response = "Se ha creado el usuario con exito y sus datos son:\n" + "Id:" + user.getId() + ", " + " Nombres: " + user.getNames() + ", " + 20 | "Email: " + user.getEmail() + ", " + "Teléfono: " + user.getPhone(); 21 | //int id = users.size()+ 1; 22 | //user.setId(id);//sino se le asigna el id, el id siempre será el mismo 23 | //user.setId(User.getId()+1);//incrementamos el id automaticamente cada vez que se cree un nuevo usuario 24 | users.add(user);//adicionamos el objeto 25 | System.out.println(response); 26 | } 27 | 28 | 29 | @Override 30 | public List readAll() { 31 | printUsers(users); 32 | return users; 33 | } 34 | 35 | //prod = new Producto(listaProductos.get(i).getCodigo(), nombre, descrip, pCompra, pVenta, cantidad); 36 | //listaProductos.set(i, prod); // se agrega en la posicion del codigo encontrado, el nuevo objeto con sus parametros 37 | @Override 38 | public void updateById(String body, int id) throws IndexOutOfBoundsException { 39 | //int indice = users.indexOf(id); 40 | //if (indice != -1) { //si el indice es diferente de -1, es decir, si existe el usuario 41 | 42 | for (User usuario : users) { 43 | if (usuario.getId() == id) { 44 | String[] data = body.split(","); 45 | user.setNames(data[0]); 46 | user.setEmail(data[1]); 47 | user.setPhone(data[2]); 48 | users.set(id, user); 49 | //user.setId(id);//reconfirmamos el id 50 | break; 51 | } 52 | }//end for 53 | System.out.println("Se ha actualizado el usuario con éxito y sus datos son:\n" + "Id:" + user.getId() + ", " + " Nombres: " + user.getNames() + ", " + 54 | "Email: " + user.getEmail() + ", " + "Teléfono: " + user.getPhone()); 55 | //} else { 56 | // System.out.println("No se ha encontrado el usuario con el id: " + id); 57 | // } 58 | } 59 | 60 | 61 | @Override 62 | public void deleteById(int id) throws IndexOutOfBoundsException { 63 | boolean encontrado = false; 64 | //for (User usuario : users) { 65 | users.removeIf(p -> p.getId() == id); 66 | //if (usuario.getId() == id) { 67 | //encontrado = true; 68 | //users.remove(usuario); 69 | 70 | // break; 71 | //} 72 | //}//end for 73 | /*if(encontrado ==false) 74 | 75 | { 76 | System.out.println("No se ha encontrado el usuario con el id: " + id); 77 | };*/ 78 | System.out.println("Se ha eliminado el usuario con Id:" + id); 79 | 80 | printUsers(users); 81 | 82 | } 83 | 84 | @Override 85 | public void findAll() { 86 | 87 | } 88 | 89 | @Override 90 | public void findById(int id) { 91 | for (User usuario : users) { 92 | if (usuario.getId() == id) { 93 | System.out.println("Se ha encontrado el usuario con Id:" + id); 94 | System.out.println(usuario.toString()); 95 | }//end if 96 | }//end for 97 | } 98 | 99 | static void printUsers(List users) { 100 | System.out.println("Lista de usuarios:"); 101 | for (int i = 0; i < users.size(); i++) { 102 | System.out.println("\n{\n\t\"id\":" + users.get(i).getId() + "," + "\n\t\"nombres\":" + users.get(i).getNames() + "\"," + 103 | "\n\t\"email\":" + "\"" + users.get(i).getEmail() + "\",\n\t\"phone\":" + "\"" + users.get(i).getPhone() + "\"\n\t\t\t\t\t\t},"); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/controller/Server.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import Service.JPAUsers; 4 | import Service.JPAUsersImpl; 5 | import model.User; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public class Server { 11 | private String method; 12 | private String url = "http://localhost:8080/api/users"; 13 | private final String versionProtocol = "HTTP/1.1"; 14 | private String headers; 15 | 16 | private String body; 17 | private int id;//para consultas por Id 18 | 19 | @Autowired 20 | private JPAUsers user;//principio de Inversión de Dependencias (IoD) 21 | 22 | public Server(String method, String url, String headers, String body) { 23 | this.method = method; 24 | this.url = url; 25 | this.headers = headers; 26 | this.body = body; 27 | this.user = new JPAUsersImpl();//una referencia q se ha creado en el constructor de la clase Server JPAUsersImpl 28 | //validamos que tipo de Método nos llegó al servidor 29 | 30 | switch (method) { 31 | case "GET": 32 | if (url.equals("http://localhost:8080/api/users")) { 33 | GetUsers(); 34 | } 35 | if (url.equals("http://localhost:8080/api/users")) { 36 | GetUserById(1); 37 | } 38 | break; 39 | case "POST": 40 | if (url.equals("http://localhost:8080/api/users")) { 41 | PostUser(body); 42 | } 43 | break; 44 | 45 | 46 | default: 47 | System.out.println("Método no soportado"); 48 | break; 49 | } 50 | 51 | 52 | }//fin si 53 | 54 | 55 | public Server(String method, String url, String headers, String body, int id) { 56 | this.method = method; 57 | this.url = url; 58 | this.headers = headers; 59 | this.body = body; 60 | this.id = id; 61 | this.user = new JPAUsersImpl();//una referencia q se ha creado en el constructor de la clase Server JPAUsersImpl 62 | 63 | switch (method) { 64 | case "PUT": 65 | if (url.equals("http://localhost:8080/api/users")) { 66 | PutUserById(body, id); 67 | } 68 | break; 69 | case "DELETE": 70 | if (url.equals("http://localhost:8080/api/users")) { 71 | DeleteUserById(id); 72 | } 73 | break; 74 | case "GET": 75 | if (url.equals("http://localhost:8080/api/users")) { 76 | GetUserById(id); 77 | } 78 | break; 79 | default: 80 | System.out.println("Método no soportado"); 81 | break; 82 | } 83 | 84 | 85 | }//end constructor 86 | 87 | 88 | String GetUsers() { 89 | user.readAll(); 90 | String response = versionProtocol + " 200 Ok\r\n" + 91 | "Content-Type: application/json\r\n" + 92 | "Content-Length: " + body.length() + "\r\n" + 93 | "\r\n" + 94 | body; 95 | return response; 96 | 97 | } 98 | 99 | String GetUserById(int id) { 100 | String response = versionProtocol + " 200 OK\n" + 101 | "Content-Type: application/json\r\n" + 102 | "Content-Length: " + id + "\r\n"; 103 | user.findById(id); 104 | return response; 105 | } 106 | 107 | public String PostUser(@RequestBody String body) { 108 | user.create(body);//acá estamos haciendo la inyección de dependencias 109 | 110 | String response = versionProtocol + " 201 Created\r\n" + 111 | "Content-Type: application/json\r\n" + 112 | "Content-Length: " + body.length() + "\r\n" + 113 | "\r\n" + 114 | body; 115 | return response; 116 | } 117 | 118 | 119 | private void DeleteUserById(int i) { 120 | user.deleteById(id); 121 | } 122 | 123 | private void PutUserById(@RequestBody String body, @RequestBody int i) { 124 | user.updateById(body, id); 125 | } 126 | 127 | 128 | @Override 129 | public String toString() { 130 | return "Server{" + 131 | "url='" + url + '\'' + 132 | ", headers='" + headers + '\'' + 133 | ", method='" + method + '\'' + 134 | ",\n body='" + body + '\'' + 135 | 136 | '}'; 137 | 138 | } 139 | 140 | 141 | } --------------------------------------------------------------------------------