Skip to content

Commit d20ac06

Browse files
authored
Complete secured Employee REST API with Spring Boot and BCrypt
1 parent 73c1fe3 commit d20ac06

23 files changed

Lines changed: 964 additions & 0 deletions

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

pom.xml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>4.0.2</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.luv2code.springboot</groupId>
12+
<artifactId>cruddemo</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>cruddemo</name>
15+
<description>Demo project for Spring Boot</description>
16+
<url/>
17+
<licenses>
18+
<license/>
19+
</licenses>
20+
<developers>
21+
<developer/>
22+
</developers>
23+
<scm>
24+
<connection/>
25+
<developerConnection/>
26+
<tag/>
27+
<url/>
28+
</scm>
29+
<properties>
30+
<java.version>25</java.version>
31+
</properties>
32+
<dependencies>
33+
34+
<dependency>
35+
<groupId>org.springdoc</groupId>
36+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
37+
<version>3.0.1</version>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-web</artifactId>
43+
</dependency>
44+
45+
46+
<!-- Spring Security -->
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-security</artifactId>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-data-jpa</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-webmvc</artifactId>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-devtools</artifactId>
64+
<scope>runtime</scope>
65+
<optional>true</optional>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.mysql</groupId>
69+
<artifactId>mysql-connector-j</artifactId>
70+
<scope>runtime</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-starter-data-jpa-test</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.springframework.boot</groupId>
79+
<artifactId>spring-boot-starter-webmvc-test</artifactId>
80+
<scope>test</scope>
81+
</dependency>
82+
</dependencies>
83+
84+
<build>
85+
<plugins>
86+
<plugin>
87+
<groupId>org.springframework.boot</groupId>
88+
<artifactId>spring-boot-maven-plugin</artifactId>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
93+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.luv2code.springboot.cruddemo;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
8+
@SpringBootApplication
9+
public class CruddemoApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(CruddemoApplication.class, args);
13+
}
14+
15+
@Bean
16+
public ObjectMapper objectMapper() {
17+
return new ObjectMapper();
18+
}
19+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.luv2code.springboot.cruddemo.dto;
2+
3+
import jakarta.validation.constraints.Email;
4+
import jakarta.validation.constraints.NotBlank;
5+
import jakarta.validation.constraints.Size;
6+
7+
public class EmployeeDTO {
8+
9+
private int id;
10+
11+
@NotBlank(message = "First name is required")
12+
@Size(max = 50, message = "First name must be at most 50 characters")
13+
private String firstName;
14+
15+
@NotBlank(message = "Last name is required")
16+
@Size(max = 50, message = "Last name must be at most 50 characters")
17+
private String lastName;
18+
19+
@NotBlank(message = "Email is required")
20+
@Email(message = "Email must be a valid email address")
21+
private String email;
22+
23+
public EmployeeDTO() {}
24+
25+
public EmployeeDTO(int id, String firstName, String lastName, String email) {
26+
this.id = id;
27+
this.firstName = firstName;
28+
this.lastName = lastName;
29+
this.email = email;
30+
}
31+
32+
// getters and setters
33+
34+
public int getId() {
35+
return id;
36+
}
37+
38+
public void setId(int id) {
39+
this.id = id;
40+
}
41+
42+
public String getFirstName() {
43+
return firstName;
44+
}
45+
46+
public void setFirstName(String firstName) {
47+
this.firstName = firstName;
48+
}
49+
50+
public String getLastName() {
51+
return lastName;
52+
}
53+
54+
public void setLastName(String lastName) {
55+
this.lastName = lastName;
56+
}
57+
58+
public String getEmail() {
59+
return email;
60+
}
61+
62+
public void setEmail(String email) {
63+
this.email = email;
64+
}
65+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.luv2code.springboot.cruddemo.dto;
2+
3+
import com.luv2code.springboot.cruddemo.entity.Employee;
4+
5+
public class EmployeeMapper {
6+
7+
public static EmployeeDTO toDTO(Employee employee) {
8+
if (employee == null) {
9+
return null;
10+
}
11+
12+
return new EmployeeDTO(
13+
employee.getId(),
14+
employee.getFirstName(),
15+
employee.getLastName(),
16+
employee.getEmail()
17+
);
18+
}
19+
20+
public static Employee toEntity(EmployeeDTO dto) {
21+
22+
if (dto == null) {
23+
return null;
24+
}
25+
26+
Employee employee = new Employee();
27+
employee.setId(dto.getId());
28+
employee.setFirstName(dto.getFirstName());
29+
employee.setLastName(dto.getLastName());
30+
employee.setEmail(dto.getEmail());
31+
return employee;
32+
}
33+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.luv2code.springboot.cruddemo.entity;
2+
3+
import jakarta.persistence.*;
4+
5+
@Entity
6+
@Table
7+
public class Employee {
8+
//define fields
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.IDENTITY)
11+
@Column(name="id")
12+
private int id;
13+
14+
@Column(name="first_name")
15+
private String firstName;
16+
17+
@Column(name="last_name")
18+
private String lastName;
19+
20+
@Column(nullable = false, unique = true)
21+
private String email;
22+
23+
24+
25+
//define constructors
26+
public Employee() {}
27+
28+
public Employee(String firstName, String lastName, String email) {
29+
this.firstName = firstName;
30+
this.lastName = lastName;
31+
this.email = email;
32+
}
33+
34+
//define getters and setters
35+
36+
public int getId() {
37+
return id;
38+
}
39+
40+
public void setId(int id) {
41+
this.id = id;
42+
}
43+
44+
public String getFirstName() {
45+
return firstName;
46+
}
47+
48+
public void setFirstName(String firstName) {
49+
this.firstName = firstName;
50+
}
51+
52+
public String getLastName() {
53+
return lastName;
54+
}
55+
56+
public void setLastName(String lastName) {
57+
this.lastName = lastName;
58+
}
59+
60+
public String getEmail() {
61+
return email;
62+
}
63+
64+
public void setEmail(String email) {
65+
this.email = email;
66+
}
67+
68+
//define toString
69+
70+
@Override
71+
public String toString() {
72+
return "Employee{" +
73+
"id=" + id +
74+
", firstName='" + firstName + '\'' +
75+
", lastName='" + lastName + '\'' +
76+
", email='" + email + '\'' +
77+
'}';
78+
}
79+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.luv2code.springboot.cruddemo.exception;
2+
3+
public class EmployeeErrorResponse {
4+
5+
private int status;
6+
private String message;
7+
private long timestamp;
8+
9+
public EmployeeErrorResponse(){
10+
11+
}
12+
13+
public EmployeeErrorResponse(int status, String message, long timestamp) {
14+
this.status = status;
15+
this.message = message;
16+
this.timestamp = timestamp;
17+
}
18+
19+
public int getStatus() {
20+
return status;
21+
}
22+
23+
public void setStatus(int status) {
24+
this.status = status;
25+
}
26+
27+
public String getMessage() {
28+
return message;
29+
}
30+
31+
public void setMessage(String message) {
32+
this.message = message;
33+
}
34+
35+
public long getTimestamp() {
36+
return timestamp;
37+
}
38+
39+
public void setTimestamp(long timestamp) {
40+
this.timestamp = timestamp;
41+
}
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.luv2code.springboot.cruddemo.exception;
2+
3+
public class EmployeeNotFoundException extends RuntimeException {
4+
public EmployeeNotFoundException(String message) {
5+
super(message);
6+
}
7+
public EmployeeNotFoundException(String message, Throwable cause) {
8+
super(message, cause);
9+
}
10+
public EmployeeNotFoundException(Throwable cause) {
11+
super(cause);
12+
}
13+
}

0 commit comments

Comments
 (0)