├── buildspec.yml ├── pom.xml ├── readme.md └── src ├── .DS_Store ├── main ├── .DS_Store ├── java │ ├── .DS_Store │ └── com │ │ ├── .DS_Store │ │ └── in28minutes │ │ ├── .DS_Store │ │ └── rest │ │ ├── .DS_Store │ │ └── webservices │ │ ├── .DS_Store │ │ └── restfulwebservices │ │ ├── .DS_Store │ │ ├── RestfulWebServicesApplication.java │ │ ├── helloworld │ │ ├── HelloWorldBean.java │ │ └── HelloWorldController.java │ │ └── todo │ │ ├── Todo.java │ │ ├── TodoJpaRepository.java │ │ └── TodoJpaResource.java └── resources │ ├── application.properties │ └── data.sql └── test └── java └── com └── in28minutes └── rest └── webservices └── restfulwebservices └── RestfulWebServicesApplicationTests.java /buildspec.yml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | 3 | phases: 4 | install: 5 | runtime-versions: 6 | java: openjdk8 7 | commands: 8 | - echo install 9 | pre_build: 10 | commands: 11 | - echo pre_build 12 | build: 13 | commands: 14 | - mvn package 15 | - echo build 16 | post_build: 17 | commands: 18 | - echo post_build 19 | 20 | artifacts: 21 | files: 22 | - target/spring-boot-todo-rest-api-h2-aws-codepipeline-0.0.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.in28minutes.rest.webservices 7 | spring-boot-todo-rest-api-h2-aws-codepipeline 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | Demo project for Spring Boot 12 | 13 | 14 | org.springframework.boot 15 | spring-boot-starter-parent 16 | 2.1.0.RELEASE 17 | 18 | 19 | 20 | 21 | UTF-8 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-jpa 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-devtools 41 | runtime 42 | 43 | 44 | 45 | com.h2database 46 | h2 47 | runtime 48 | 49 | 50 | 51 | 52 | 53 | javax.xml.bind 54 | jaxb-api 55 | 2.3.0 56 | 57 | 58 | com.sun.xml.bind 59 | jaxb-impl 60 | 2.3.0 61 | 62 | 63 | org.glassfish.jaxb 64 | jaxb-runtime 65 | 2.3.0 66 | 67 | 68 | javax.activation 69 | activation 70 | 1.1.1 71 | 72 | 73 | 74 | org.springframework.boot 75 | spring-boot-starter-test 76 | test 77 | 78 | 79 | 80 | 81 | 82 | 83 | org.springframework.boot 84 | spring-boot-maven-plugin 85 | 86 | 87 | 88 | 89 | 90 | 91 | spring-snapshots 92 | Spring Snapshots 93 | https://repo.spring.io/snapshot 94 | 95 | true 96 | 97 | 98 | 99 | spring-milestones 100 | Spring Milestones 101 | https://repo.spring.io/milestone 102 | 103 | false 104 | 105 | 106 | 107 | 108 | 109 | 110 | spring-snapshots 111 | Spring Snapshots 112 | https://repo.spring.io/snapshot 113 | 114 | true 115 | 116 | 117 | 118 | spring-milestones 119 | Spring Milestones 120 | https://repo.spring.io/milestone 121 | 122 | false 123 | 124 | 125 | 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Todo and Hello World Rest APIs Connecting to H2 In memory database running on port 5000 2 | 3 | Run com.in28minutes.rest.webservices.restfulwebservices.RestfulWebServicesApplication as a Java Application. 4 | 5 | 6 | ## Hello World Resource 7 | 8 | - http://localhost:5000/hello-world 9 | 10 | ```txt 11 | Hello World 12 | ``` 13 | 14 | - http://localhost:5000/hello-world-bean 15 | 16 | ```json 17 | {"message":"Hello World - Changed"} 18 | ``` 19 | 20 | - http://localhost:5000/hello-world/path-variable/in28minutes 21 | 22 | ```json 23 | {"message":"Hello World, in28minutes"} 24 | ``` 25 | 26 | 27 | ## Todo Resource 28 | 29 | - GET - http://localhost:5000/jpa/users/in28minutes/todos 30 | 31 | ``` 32 | [ 33 | { 34 | "id": 10001, 35 | "username": "in28minutes", 36 | "description": "Learn JPA", 37 | "targetDate": "2019-06-27T06:30:30.696+0000", 38 | "done": false 39 | }, 40 | { 41 | "id": 10002, 42 | "username": "in28minutes", 43 | "description": "Learn Data JPA", 44 | "targetDate": "2019-06-27T06:30:30.700+0000", 45 | "done": false 46 | }, 47 | { 48 | "id": 10003, 49 | "username": "in28minutes", 50 | "description": "Learn Microservices", 51 | "targetDate": "2019-06-27T06:30:30.701+0000", 52 | "done": false 53 | } 54 | ] 55 | ``` 56 | 57 | #### Retrieve a specific todo 58 | 59 | - GET - http://localhost:5000/jpa/users/in28minutes/todos/10001 60 | 61 | ``` 62 | { 63 | "id": 10001, 64 | "username": "in28minutes", 65 | "description": "Learn JPA", 66 | "targetDate": "2019-06-27T06:30:30.696+0000", 67 | "done": false 68 | } 69 | ``` 70 | 71 | #### Creating a new todo 72 | 73 | 74 | 75 | - POST to http://localhost:5000/jpa/users/in28minutes/todos with BODY of Request given below 76 | 77 | ``` 78 | { 79 | "username": "in28minutes", 80 | "description": "Learn to Drive a Car", 81 | "targetDate": "2030-11-09T10:49:23.566+0000", 82 | "done": false 83 | } 84 | ``` 85 | 86 | #### Updating an existing todo 87 | 88 | - PUT Request to http://localhost:5000/jpa/users/in28minutes/todos/10001 with BODY of Request given below 89 | 90 | ``` 91 | { 92 | "id": 10001, 93 | "username": "in28minutes", 94 | "description": "Learn to Drive a Car", 95 | "targetDate": "2045-11-09T10:49:23.566+0000", 96 | "done": false 97 | } 98 | ``` 99 | 100 | #### Delete todo 101 | 102 | - DELETE to http://localhost:5000/jpa/users/in28minutes/todos/10001 103 | 104 | 105 | ## H2 Console 106 | 107 | - http://localhost:5000/h2-console 108 | - Use `jdbc:h2:mem:testdb` as JDBC URL 109 | -------------------------------------------------------------------------------- /src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in28minutes/spring-boot-todo-rest-api-h2-aws-codepipeline/0eb9abc2187be3a0ccd8b0fd2d858fd77d2ceb04/src/.DS_Store -------------------------------------------------------------------------------- /src/main/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in28minutes/spring-boot-todo-rest-api-h2-aws-codepipeline/0eb9abc2187be3a0ccd8b0fd2d858fd77d2ceb04/src/main/.DS_Store -------------------------------------------------------------------------------- /src/main/java/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in28minutes/spring-boot-todo-rest-api-h2-aws-codepipeline/0eb9abc2187be3a0ccd8b0fd2d858fd77d2ceb04/src/main/java/.DS_Store -------------------------------------------------------------------------------- /src/main/java/com/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in28minutes/spring-boot-todo-rest-api-h2-aws-codepipeline/0eb9abc2187be3a0ccd8b0fd2d858fd77d2ceb04/src/main/java/com/.DS_Store -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in28minutes/spring-boot-todo-rest-api-h2-aws-codepipeline/0eb9abc2187be3a0ccd8b0fd2d858fd77d2ceb04/src/main/java/com/in28minutes/.DS_Store -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/rest/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in28minutes/spring-boot-todo-rest-api-h2-aws-codepipeline/0eb9abc2187be3a0ccd8b0fd2d858fd77d2ceb04/src/main/java/com/in28minutes/rest/.DS_Store -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/rest/webservices/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in28minutes/spring-boot-todo-rest-api-h2-aws-codepipeline/0eb9abc2187be3a0ccd8b0fd2d858fd77d2ceb04/src/main/java/com/in28minutes/rest/webservices/.DS_Store -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/rest/webservices/restfulwebservices/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/in28minutes/spring-boot-todo-rest-api-h2-aws-codepipeline/0eb9abc2187be3a0ccd8b0fd2d858fd77d2ceb04/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/.DS_Store -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/rest/webservices/restfulwebservices/RestfulWebServicesApplication.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.rest.webservices.restfulwebservices; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class RestfulWebServicesApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(RestfulWebServicesApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/rest/webservices/restfulwebservices/helloworld/HelloWorldBean.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.rest.webservices.restfulwebservices.helloworld; 2 | 3 | public class HelloWorldBean { 4 | 5 | private String message; 6 | 7 | public HelloWorldBean(String message) { 8 | this.message = message; 9 | } 10 | 11 | public String getMessage() { 12 | return message; 13 | } 14 | 15 | public void setMessage(String message) { 16 | this.message = message; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return String.format("HelloWorldBean [message=%s]", message); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/rest/webservices/restfulwebservices/helloworld/HelloWorldController.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.rest.webservices.restfulwebservices.helloworld; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.PathVariable; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | //Controller 8 | @RestController 9 | public class HelloWorldController { 10 | 11 | @GetMapping(path = "/hello-world") 12 | public String helloWorld() { 13 | return "Hello World v2"; 14 | } 15 | 16 | @GetMapping(path = "/hello-world-bean") 17 | public HelloWorldBean helloWorldBean() { 18 | return new HelloWorldBean("Hello World"); 19 | } 20 | 21 | ///hello-world/path-variable/in28minutes 22 | @GetMapping(path = "/hello-world/path-variable/{name}") 23 | public HelloWorldBean helloWorldPathVariable(@PathVariable String name) { 24 | //throw new RuntimeException("Something went wrong"); 25 | return new HelloWorldBean(String.format("Hello World, %s", name)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/rest/webservices/restfulwebservices/todo/Todo.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.rest.webservices.restfulwebservices.todo; 2 | 3 | import java.util.Date; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.Id; 8 | 9 | @Entity 10 | public class Todo { 11 | @Id 12 | @GeneratedValue 13 | private Long id; 14 | private String username; 15 | private String description; 16 | private Date targetDate; 17 | private boolean isDone; 18 | 19 | public Todo() { 20 | 21 | } 22 | 23 | public Todo(long id, String username, String description, Date targetDate, boolean isDone) { 24 | super(); 25 | this.id = id; 26 | this.username = username; 27 | this.description = description; 28 | this.targetDate = targetDate; 29 | this.isDone = isDone; 30 | } 31 | 32 | public Long getId() { 33 | return id; 34 | } 35 | 36 | public void setId(Long id) { 37 | this.id = id; 38 | } 39 | 40 | public String getUsername() { 41 | return username; 42 | } 43 | 44 | public void setUsername(String username) { 45 | this.username = username; 46 | } 47 | 48 | public String getDescription() { 49 | return description; 50 | } 51 | 52 | public void setDescription(String description) { 53 | this.description = description; 54 | } 55 | 56 | public Date getTargetDate() { 57 | return targetDate; 58 | } 59 | 60 | public void setTargetDate(Date targetDate) { 61 | this.targetDate = targetDate; 62 | } 63 | 64 | public boolean isDone() { 65 | return isDone; 66 | } 67 | 68 | public void setDone(boolean isDone) { 69 | this.isDone = isDone; 70 | } 71 | 72 | @Override 73 | public int hashCode() { 74 | final int prime = 31; 75 | int result = 1; 76 | result = prime * result + (int) (id ^ (id >>> 32)); 77 | return result; 78 | } 79 | 80 | @Override 81 | public boolean equals(Object obj) { 82 | if (this == obj) 83 | return true; 84 | if (obj == null) 85 | return false; 86 | if (getClass() != obj.getClass()) 87 | return false; 88 | Todo other = (Todo) obj; 89 | if (id != other.id) 90 | return false; 91 | return true; 92 | } 93 | 94 | 95 | } -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/rest/webservices/restfulwebservices/todo/TodoJpaRepository.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.rest.webservices.restfulwebservices.todo; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | @Repository 9 | public interface TodoJpaRepository extends JpaRepository{ 10 | List findByUsername(String username); 11 | } -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/rest/webservices/restfulwebservices/todo/TodoJpaResource.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.rest.webservices.restfulwebservices.todo; 2 | 3 | import java.net.URI; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.DeleteMapping; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.PostMapping; 13 | import org.springframework.web.bind.annotation.PutMapping; 14 | import org.springframework.web.bind.annotation.RequestBody; 15 | import org.springframework.web.bind.annotation.RestController; 16 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder; 17 | 18 | @RestController 19 | public class TodoJpaResource { 20 | 21 | @Autowired 22 | private TodoJpaRepository todoJpaRepository; 23 | 24 | 25 | @GetMapping("/jpa/users/{username}/todos") 26 | public List getAllTodos(@PathVariable String username){ 27 | return todoJpaRepository.findByUsername(username); 28 | } 29 | 30 | @GetMapping("/jpa/users/{username}/todos/{id}") 31 | public Todo getTodo(@PathVariable String username, @PathVariable long id){ 32 | return todoJpaRepository.findById(id).get(); 33 | } 34 | 35 | @DeleteMapping("/jpa/users/{username}/todos/{id}") 36 | public ResponseEntity deleteTodo( 37 | @PathVariable String username, @PathVariable long id) { 38 | 39 | todoJpaRepository.deleteById(id); 40 | 41 | return ResponseEntity.noContent().build(); 42 | } 43 | 44 | 45 | @PutMapping("/jpa/users/{username}/todos/{id}") 46 | public ResponseEntity updateTodo( 47 | @PathVariable String username, 48 | @PathVariable long id, @RequestBody Todo todo){ 49 | 50 | todo.setUsername(username); 51 | 52 | Todo todoUpdated = todoJpaRepository.save(todo); 53 | 54 | return new ResponseEntity(todoUpdated, HttpStatus.OK); 55 | } 56 | 57 | @PostMapping("/jpa/users/{username}/todos") 58 | public ResponseEntity createTodo( 59 | @PathVariable String username, @RequestBody Todo todo){ 60 | 61 | todo.setId(-1L); 62 | 63 | Todo createdTodo = todoJpaRepository.save(todo); 64 | 65 | //Location 66 | //Get current resource url 67 | ///{id} 68 | URI uri = ServletUriComponentsBuilder.fromCurrentRequest() 69 | .path("/{id}").buildAndExpand(createdTodo.getId()).toUri(); 70 | 71 | return ResponseEntity.created(uri).build(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jpa.show-sql=true 2 | spring.h2.console.enabled=true 3 | spring.h2.console.settings.web-allow-others=true 4 | 5 | logging.level.org.springframework = info 6 | server.port=5000 7 | -------------------------------------------------------------------------------- /src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | insert into todo(id, username,description,target_date,is_done) 2 | values(10001, 'in28minutes', 'Learn JPA', sysdate(), false); 3 | 4 | insert into todo(id, username,description,target_date,is_done) 5 | values(10002, 'in28minutes', 'Learn Data JPA', sysdate(), false); 6 | 7 | insert into todo(id, username,description,target_date,is_done) 8 | values(10003, 'in28minutes', 'Learn Microservices', sysdate(), false); -------------------------------------------------------------------------------- /src/test/java/com/in28minutes/rest/webservices/restfulwebservices/RestfulWebServicesApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.rest.webservices.restfulwebservices; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class RestfulWebServicesApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------