-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringbootPotsIssue.txt
More file actions
162 lines (116 loc) · 2.93 KB
/
SpringbootPotsIssue.txt
File metadata and controls
162 lines (116 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 📡 Sending Data from Frontend to Spring Boot (POST Request)
## 🚨 Problem
When sending data using `fetch` from JavaScript to a Spring Boot backend, you might encounter errors like:
* `400 Bad Request`
* `HttpMessageNotReadableException`
* Backend not receiving data correctly
### ❌ Incorrect Example
```javascript
const response = await fetch("http://localhost:8080/employees/create", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: {
firstName: "alice",
lastName: "bob"
}
});
```
### 🔴 Why This Fails
* `body` is a **JavaScript object**, not JSON.
* The browser converts it to:
```
[object Object]
```
* This is **not valid JSON**, so Spring Boot cannot parse it.
---
## ✅ Solution: Use `JSON.stringify`
### ✔️ Correct Example
```javascript
const response = await fetch("http://localhost:8080/employees/create", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
firstName: "alice",
lastName: "bob",
email: "bob@gmail.com",
phoneNumber: "+21629183455",
hireDate: "2024-02-10"
})
});
const data = await response.json();
console.log(data);
```
---
## 🧠 How It Works
| Step | Explanation |
| -------------------------------- | ------------------------------------- |
| `JSON.stringify()` | Converts JS object → JSON string |
| `Content-Type: application/json` | Tells backend to expect JSON |
| Spring Boot | Automatically maps JSON → Java object |
---
## ☕ Spring Boot Backend Example
### DTO Class
```java
public class EmployeeDTO {
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private String hireDate;
// Getters & Setters
}
```
---
### Controller
```java
@PostMapping("/create")
public Employee createEmployee(@RequestBody EmployeeDTO dto) {
System.out.println(dto.getFirstName());
return employeeService.save(dto);
}
```
---
## ⚠️ Important Notes
* Field names in JSON must match Java fields:
```json
{
"firstName": "alice"
}
```
✔ Matches:
```java
private String firstName;
```
---
## 🔁 Alternative (Without JSON)
You can also send data using `FormData`:
```javascript
const formData = new FormData();
formData.append("firstName", "alice");
formData.append("lastName", "bob");
fetch("http://localhost:8080/employees/create", {
method: "POST",
body: formData
});
```
⚠️ In this case, backend must use:
```java
@RequestParam String firstName
```
---
## ✅ Summary
* ❌ Don't send plain JS objects in `fetch`
* ✅ Always use `JSON.stringify()` for JSON APIs
* ✅ Match frontend keys with backend fields
* ✅ Set correct headers
---
## 🚀 Result
After applying this:
* ✔ No more 400 errors
* ✔ Data correctly received in Spring Boot
* ✔ Clean frontend ↔ backend communication
---