π 403 μλ¬ ν΄κ²°!
π ν΅μ¬ μμΈ
GET μμ²μμ @RequestBody
λ₯Ό μ¬μ©νλ €κ³ νκΈ° λλ¬Έμ
λλ€.
π λ¬Έμ μν©
κΈ°μ‘΄ μ½λ
@GetMapping
public ResponseEntity<StudentResponseDto> getStudent(
@RequestBody String studentId // β GET μμ²μ @RequestBody μ¬μ©
) {
return ResponseEntity.ok(studentService.getStudent(studentId));
}
λ°μν μλ¬
java.lang.IllegalArgumentException: μ‘΄μ¬νμ§ μλ νμμ
λλ€.
- μ΅μ’ μλ΅: 403 Forbidden
- μμΈ λ°μ μμΉ:
StudentService
56λ²μ§Έ μ€
π‘ μμΈ λΆμ
1. HTTP GET μμ²μ νΉμ±
- GET μμ²μ 리μμ€ μ‘°νλ₯Ό μν΄ μ¬μ©
- Bodyλ₯Ό ν¬ν¨ν μ μμ§λ§, λλΆλΆμ μλ²κ° μ΄λ₯Ό 무μνλλ‘ μ€κ³λ¨
- HTTP/1.1 λͺ μΈμμλ GETμ Body μ²λ¦¬λ₯Ό κΆμ₯νμ§ μμ
2. Spring Bootμ λμ
μμ² νλ¦:
Postman GET μμ² (Body: {"studentId": "1003001"})
β
Spring MVCκ° @RequestBodyλ‘ λ°μΈλ© μλ
β
λ°μΈλ© μ€ν¨ β studentIdμ null μ λ¬
β
studentRepository.findByStudentId(null)
β
Optional.empty() β orElseThrow() νΈμΆ
β
IllegalArgumentException λ°μ
β
Spring Securityμ ExceptionTranslationFilter μ²λ¦¬
β
403 Forbidden μλ΅
3. μ£Όμ ν¬μΈνΈ
-
SecurityConfig
μμpermitAll()
μ€μ κ³Ό 무κ΄νκ² λ°μ - 보μ λ¬Έμ κ° μλ μμ² μ²λ¦¬ κ³Όμ μ λ¬Έμ
-
null
κ°μΌλ‘ μΈν λΉμ¦λμ€ λ‘μ§ μμΈκ° κ·Όλ³Έ μμΈ
β ν΄κ²° λ°©μ
1. Controller μμ
Before
@GetMapping
public ResponseEntity<StudentResponseDto> getStudent(
@RequestBody String studentId
) {
return ResponseEntity.ok(studentService.getStudent(studentId));
}
After
@GetMapping("/{studentId}")
public ResponseEntity<StudentResponseDto> getStudent(
@PathVariable String studentId // β
URL κ²½λ‘μμ κ° μΆμΆ
) {
return ResponseEntity.ok(studentService.getStudent(studentId));
}
2. λ³κ²½ μ¬ν
| νλͺ© | λ³κ²½ μ | λ³κ²½ ν |
|ββ|βββ|βββ|
| λ§€ν κ²½λ‘ | @GetMapping
| @GetMapping("/{studentId}")
|
| νλΌλ―Έν° μ΄λ
Έν
μ΄μ
| @RequestBody
| @PathVariable
|
| λ°μ΄ν° μμΉ | Request Body | URL Path |
π§ͺ ν μ€νΈ λ°©λ²
Postman μ€μ
Method: GET
URL: http://localhost:8080/api/v1/management/1003001
βββββββββββββββββββββββββββββββββββββββ¬ββββββ
studentId κ°
Body: None
μμ κ²°κ³Ό
{
"studentId": "1003001",
"name": "νκΈΈλ",
...
}
π μ°Έκ³ : GET μμ² νλΌλ―Έν° μ λ¬ λ°©μ
λ°©μ | μ΄λ Έν μ΄μ | μ¬μ© μμ | μ ν©ν μν© |
---|---|---|---|
Path Variable | @PathVariable |
/students/{id} |
리μμ€ μλ³ (κΆμ₯) |
Query Parameter | @RequestParam |
/students?id=1003001 |
νν°λ§, κ²μ 쑰건 |
@RequestBody |
β GETμμ λΉκΆμ₯ | POST, PUT λ±μμ μ¬μ© |
π‘ Best Practice
-
λ¨μΌ 리μμ€ μ‘°ν:
@PathVariable
μ¬μ© -
λͺ©λ‘ μ‘°ν + νν°:
@RequestParam
μ¬μ© -
볡μ‘ν λ°μ΄ν° μ μ‘: POST/PUT λ©μλ +
@RequestBody
μ¬μ©