From 73022f7b851d89ab5abea179da30026ec5eaab72 Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Thu, 1 Mar 2018 08:26:12 +0530 Subject: [PATCH 01/12] Using https url for git clone --- Readme.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index ecdc1c8..7353e4e 100644 --- a/Readme.md +++ b/Readme.md @@ -15,7 +15,7 @@ Build Restful CRUD API for a simple Note-Taking application using Spring Boot, M **1. Clone the application** ```bash -git@github.com:callicoder/spring-boot-mysql-rest-api-tutorial.git +git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git ``` **2. Create Mysql database** diff --git a/pom.xml b/pom.xml index 19f9a8a..35dedad 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.4.RELEASE + 1.5.8.RELEASE From e6a2f67167abca365c63685fbf98041f554498fd Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Sun, 4 Mar 2018 08:42:14 +0530 Subject: [PATCH 02/12] Spring Boot version updated to 2.0.0 --- pom.xml | 2 +- .../easynotes/controller/NoteController.java | 37 +++++++++---------- .../exception/ResourceNotFoundException.java | 30 +++++++++++++++ 3 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/example/easynotes/exception/ResourceNotFoundException.java diff --git a/pom.xml b/pom.xml index 35dedad..9c59df4 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.8.RELEASE + 2.0.0.RELEASE diff --git a/src/main/java/com/example/easynotes/controller/NoteController.java b/src/main/java/com/example/easynotes/controller/NoteController.java index c3663d8..52f2ef0 100644 --- a/src/main/java/com/example/easynotes/controller/NoteController.java +++ b/src/main/java/com/example/easynotes/controller/NoteController.java @@ -1,5 +1,6 @@ package com.example.easynotes.controller; +import com.example.easynotes.exception.ResourceNotFoundException; import com.example.easynotes.model.Note; import com.example.easynotes.repository.NoteRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -24,42 +25,38 @@ public List getAllNotes() { return noteRepository.findAll(); } - @GetMapping("/notes/{id}") - public ResponseEntity getNoteById(@PathVariable(value = "id") Long noteId) { - Note note = noteRepository.findOne(noteId); - if(note == null) { - return ResponseEntity.notFound().build(); - } - return ResponseEntity.ok().body(note); - } - @PostMapping("/notes") public Note createNote(@Valid @RequestBody Note note) { return noteRepository.save(note); } + @GetMapping("/notes/{id}") + public Note getNoteById(@PathVariable(value = "id") Long noteId) { + return noteRepository.findById(noteId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId)); + } + @PutMapping("/notes/{id}") - public ResponseEntity updateNote(@PathVariable(value = "id") Long noteId, + public Note updateNote(@PathVariable(value = "id") Long noteId, @Valid @RequestBody Note noteDetails) { - Note note = noteRepository.findOne(noteId); - if(note == null) { - return ResponseEntity.notFound().build(); - } + + Note note = noteRepository.findById(noteId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId)); + note.setTitle(noteDetails.getTitle()); note.setContent(noteDetails.getContent()); Note updatedNote = noteRepository.save(note); - return ResponseEntity.ok(updatedNote); + return updatedNote; } @DeleteMapping("/notes/{id}") - public ResponseEntity deleteNote(@PathVariable(value = "id") Long noteId) { - Note note = noteRepository.findOne(noteId); - if(note == null) { - return ResponseEntity.notFound().build(); - } + public ResponseEntity deleteNote(@PathVariable(value = "id") Long noteId) { + Note note = noteRepository.findById(noteId) + .orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId)); noteRepository.delete(note); + return ResponseEntity.ok().build(); } } diff --git a/src/main/java/com/example/easynotes/exception/ResourceNotFoundException.java b/src/main/java/com/example/easynotes/exception/ResourceNotFoundException.java new file mode 100644 index 0000000..4e4c4f2 --- /dev/null +++ b/src/main/java/com/example/easynotes/exception/ResourceNotFoundException.java @@ -0,0 +1,30 @@ +package com.example.easynotes.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class ResourceNotFoundException extends RuntimeException { + private String resourceName; + private String fieldName; + private Object fieldValue; + + public ResourceNotFoundException( String resourceName, String fieldName, Object fieldValue) { + super(String.format("%s not found with %s : '%s'", resourceName, fieldName, fieldValue)); + this.resourceName = resourceName; + this.fieldName = fieldName; + this.fieldValue = fieldValue; + } + + public String getResourceName() { + return resourceName; + } + + public String getFieldName() { + return fieldName; + } + + public Object getFieldValue() { + return fieldValue; + } +} From e2ecdd046ba3b54b9606a49b292527b24f82a915 Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Sun, 4 Mar 2018 09:25:03 +0530 Subject: [PATCH 03/12] Using javax NotBlank constraint --- src/main/java/com/example/easynotes/model/Note.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/example/easynotes/model/Note.java b/src/main/java/com/example/easynotes/model/Note.java index 093a6b9..5823dab 100644 --- a/src/main/java/com/example/easynotes/model/Note.java +++ b/src/main/java/com/example/easynotes/model/Note.java @@ -1,12 +1,12 @@ package com.example.easynotes.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import org.hibernate.validator.constraints.NotBlank; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import javax.persistence.*; +import javax.validation.constraints.NotBlank; import java.util.Date; /** From 3b4a873721eb87ca23743af3d392c0958f1f87fe Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Tue, 6 Mar 2018 10:47:07 +0530 Subject: [PATCH 04/12] Added @Repository annotation --- .../java/com/example/easynotes/repository/NoteRepository.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/example/easynotes/repository/NoteRepository.java b/src/main/java/com/example/easynotes/repository/NoteRepository.java index 1204886..4b7b8b3 100644 --- a/src/main/java/com/example/easynotes/repository/NoteRepository.java +++ b/src/main/java/com/example/easynotes/repository/NoteRepository.java @@ -2,10 +2,13 @@ import com.example.easynotes.model.Note; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; /** * Created by rajeevkumarsingh on 27/06/17. */ + +@Repository public interface NoteRepository extends JpaRepository { } From 04f5ad4fb3bc0facf63300c847701015a55c3b98 Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Thu, 8 Mar 2018 13:16:12 +0530 Subject: [PATCH 05/12] Using GenerationType.IDENTITY --- src/main/java/com/example/easynotes/model/Note.java | 2 +- src/main/resources/application.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/easynotes/model/Note.java b/src/main/java/com/example/easynotes/model/Note.java index 5823dab..4f8b6b1 100644 --- a/src/main/java/com/example/easynotes/model/Note.java +++ b/src/main/java/com/example/easynotes/model/Note.java @@ -19,7 +19,7 @@ allowGetters = true) public class Note { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotBlank diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 41354eb..b361d7f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) spring.datasource.url = jdbc:mysql://localhost:3306/notes_app?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false spring.datasource.username = root -spring.datasource.password = root +spring.datasource.password = callicoder ## Hibernate Properties From a852cfb26198ed21c5438f40832810229b6b0d88 Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Wed, 21 Mar 2018 08:05:23 +0530 Subject: [PATCH 06/12] org.hibernate.dialect.MySQL5InnoDBDialect --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b361d7f..b0f06a8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,7 +7,7 @@ spring.datasource.password = callicoder ## Hibernate Properties # The SQL dialect makes Hibernate generate better SQL for the chosen database -spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect # Hibernate ddl auto (create, create-drop, validate, update) spring.jpa.hibernate.ddl-auto = update \ No newline at end of file From 77a4773a0f288b14700d7fd39dc7dc91a81fc4fb Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Fri, 6 Apr 2018 08:13:39 +0530 Subject: [PATCH 07/12] Readme --- Readme.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Readme.md b/Readme.md index 7353e4e..d2bf8f6 100644 --- a/Readme.md +++ b/Readme.md @@ -14,35 +14,35 @@ Build Restful CRUD API for a simple Note-Taking application using Spring Boot, M **1. Clone the application** -```bash -git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git -``` + ```bash + git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git + ``` **2. Create Mysql database** -```bash -create database notes_app -``` + ```bash + create database notes_app + ``` **3. Change mysql username and password as per your installation** -+ open `src/main/resources/application.properties` + + open `src/main/resources/application.properties` -+ change `spring.datasource.username` and `spring.datasource.password` as per your mysql installation + + change `spring.datasource.username` and `spring.datasource.password` as per your mysql installation **2. Build and run the app using maven** -```bash -mvn package -java -jar target/easy-notes-1.0.0.jar -``` + ```bash + mvn package + java -jar target/easy-notes-1.0.0.jar + ``` -Alternatively, you can run the app without packaging it using - + Alternatively, you can run the app without packaging it using - -```bash -mvn spring-boot:run -``` + ```bash + mvn spring-boot:run + ``` -The app will start running at . + The app will start running at . ## Explore Rest APIs From 5a6eb18120ab4d4bf6b151c071e4b32e1b3f2424 Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Fri, 6 Apr 2018 08:14:46 +0530 Subject: [PATCH 08/12] Readme --- Readme.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index d2bf8f6..a633d12 100644 --- a/Readme.md +++ b/Readme.md @@ -14,11 +14,12 @@ Build Restful CRUD API for a simple Note-Taking application using Spring Boot, M **1. Clone the application** - ```bash - git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git - ``` + ```bash + git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git + ``` **2. Create Mysql database** + ```bash create database notes_app ``` From 41fb5894e26d5df46fe04b8c6552202dde2725a2 Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Fri, 6 Apr 2018 08:15:22 +0530 Subject: [PATCH 09/12] Readme --- Readme.md | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/Readme.md b/Readme.md index a633d12..dd07c08 100644 --- a/Readme.md +++ b/Readme.md @@ -14,36 +14,35 @@ Build Restful CRUD API for a simple Note-Taking application using Spring Boot, M **1. Clone the application** - ```bash - git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git - ``` +```bash +git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git +``` **2. Create Mysql database** - - ```bash - create database notes_app - ``` +```bash +create database notes_app +``` **3. Change mysql username and password as per your installation** - + open `src/main/resources/application.properties` ++ open `src/main/resources/application.properties` - + change `spring.datasource.username` and `spring.datasource.password` as per your mysql installation ++ change `spring.datasource.username` and `spring.datasource.password` as per your mysql installation -**2. Build and run the app using maven** +**4. Build and run the app using maven** - ```bash - mvn package - java -jar target/easy-notes-1.0.0.jar - ``` +```bash +mvn package +java -jar target/easy-notes-1.0.0.jar +``` - Alternatively, you can run the app without packaging it using - +Alternatively, you can run the app without packaging it using - - ```bash - mvn spring-boot:run - ``` +```bash +mvn spring-boot:run +``` - The app will start running at . +The app will start running at . ## Explore Rest APIs From 40bc236289635613f843f913836f16e83740788d Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Tue, 17 Jul 2018 18:14:39 +0530 Subject: [PATCH 10/12] Added an Index endpoint --- .gitignore | 3 ++- .../easynotes/controller/IndexController.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/easynotes/controller/IndexController.java diff --git a/.gitignore b/.gitignore index 2af7cef..ff29930 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ build/ nbbuild/ dist/ nbdist/ -.nb-gradle/ \ No newline at end of file +.nb-gradle/ +.elasticbeanstalk diff --git a/src/main/java/com/example/easynotes/controller/IndexController.java b/src/main/java/com/example/easynotes/controller/IndexController.java new file mode 100644 index 0000000..f9c5ea4 --- /dev/null +++ b/src/main/java/com/example/easynotes/controller/IndexController.java @@ -0,0 +1,16 @@ +package com.example.easynotes.controller; + + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/") +public class IndexController { + + @GetMapping + public String sayHello() { + return "Hello and Welcome to the EasyNotes application. You can create a new Note by making a POST request to /api/notes endpoint."; + } +} From c03902bd66409c62cec0c6c6567e71cb88d4c688 Mon Sep 17 00:00:00 2001 From: Rajeev Singh Date: Tue, 26 Nov 2019 22:10:05 +0530 Subject: [PATCH 11/12] Bump up spring boot version to 2.2.1 --- pom.xml | 2 +- src/main/resources/application.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 9c59df4..1145d6d 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.RELEASE + 2.2.1.RELEASE diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b0f06a8..d357d46 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url = jdbc:mysql://localhost:3306/notes_app?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false +spring.datasource.url = jdbc:mysql://localhost:3306/notes_app?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false spring.datasource.username = root spring.datasource.password = callicoder From 842e64455ce9bc2dee07299c4acaf748e165de9c Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Singh Date: Wed, 13 Oct 2021 22:32:16 +0530 Subject: [PATCH 12/12] Bump up Sprint Boot version to 2.5.5 --- pom.xml | 9 ++++++--- .../com/example/easynotes/EasyNotesApplicationTests.java | 5 +---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 1145d6d..baec0d9 100644 --- a/pom.xml +++ b/pom.xml @@ -14,14 +14,14 @@ org.springframework.boot spring-boot-starter-parent - 2.2.1.RELEASE + 2.5.5 UTF-8 UTF-8 - 1.8 + 11 @@ -33,7 +33,10 @@ org.springframework.boot spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-devtools diff --git a/src/test/java/com/example/easynotes/EasyNotesApplicationTests.java b/src/test/java/com/example/easynotes/EasyNotesApplicationTests.java index 4bf539c..298ed22 100644 --- a/src/test/java/com/example/easynotes/EasyNotesApplicationTests.java +++ b/src/test/java/com/example/easynotes/EasyNotesApplicationTests.java @@ -1,11 +1,8 @@ package com.example.easynotes; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringRunner.class) @SpringBootTest public class EasyNotesApplicationTests {