spring-attic / tut-react-and-spring-data-rest

React.js and Spring Data REST :: A tutorial based on the 5-part blog series by Greg Turnquist
https://spring.io/guides/tutorials/react-and-spring-data-rest
882 stars 1.58k forks source link

Part 5: Touring your secured REST service #111

Open DominikZig opened 5 years ago

DominikZig commented 5 years ago

Hi,

I just finished Part 5 of the tutorial, but I had trouble curling the first employee id as shown in the example under Touring you secured REST service. In the example, it says to "curl -v -u greg:turnquist localhost:8080/api/employees/1", which returns Frodo Baggins (as he's the first employee in the database). However, for me, when I try and curl the employee id 1, it gives me a 404 instead of returning the Frodo Baggins employee (the same happens for employee id 2 as well). I can successfully retrieve Frodo Baggins when I curl id 3 though, which returns the employee details as normal.

Essentially, my employees id's seem to be starting at 3 and not 1. My theory is maybe this is due to the two managers being created who also have id's and they're being created first?

maldonadojsm commented 3 years ago

Yeah, I'm having the same issues. My employee count is starting at 3 rather than at 1

yukihane commented 2 years ago

When change @GeneratedValue strategy AUTO to IDENTITY, it will work expected.

diff --git a/security/src/main/java/com/greglturnquist/payroll/Employee.java b/security/src/main/java/com/greglturnquist/payroll/Employee.java
index 9f7950d..90690ca 100644
--- a/security/src/main/java/com/greglturnquist/payroll/Employee.java
+++ b/security/src/main/java/com/greglturnquist/payroll/Employee.java
@@ -19,6 +19,7 @@ import java.util.Objects;

 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.ManyToOne;
 import javax.persistence.Version;
@@ -32,7 +33,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
 @Entity
 public class Employee {

-   private @Id @GeneratedValue Long id;
+   private @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id;
    private String firstName;
    private String lastName;
    private String description;
diff --git a/security/src/main/java/com/greglturnquist/payroll/Manager.java b/security/src/main/java/com/greglturnquist/payroll/Manager.java
index 2445690..fcba774 100644
--- a/security/src/main/java/com/greglturnquist/payroll/Manager.java
+++ b/security/src/main/java/com/greglturnquist/payroll/Manager.java
@@ -20,6 +20,7 @@ import java.util.Objects;

 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;

 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@@ -36,7 +37,7 @@ public class Manager {

    public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder(); // <1>

-   private @Id @GeneratedValue Long id; // <2>
+   private @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; // <2>

    private String name; // <2>