π DTO μ€κ³!
π― ν΅μ¬ μ΄μ
μμ±(Create) μ μ¬μ©νλ StudentRequestDto
λ₯Ό μμ (Update) APIμμ μ¬μ¬μ©νκ³ μμ΄, API μ¬μ©μμκ² νΌλμ μ€ μ μμ΅λλ€.
β οΈ νμ¬ μ½λμ λ¬Έμ μ
1. API μλκ° λΆλͺ ν
// StudentRequestDtoλ μ¬λ¬ νλλ₯Ό ν¬ν¨
- name
- admissionYear
- majorName
- doubleMajorName
API μ¬μ©μμ νΌλ
- βμ΄λ¦λ§ 보λ΄λ©΄ λλ?β
- βλ€λ₯Έ νλλ€λ μ±μμΌ νλ?β
- βnullλ‘ λ³΄λ΄λ©΄ μ΄λ»κ² λμ§?β
2. λΆνμν λ°μ΄ν° μ μ‘
ν΄λΌμ΄μΈνΈκ° μ΄λ¦λ§ μμ νκ³ μΆμ΄λ, λ€λ₯Έ νλλ€μ JSONμ ν¬ν¨ν μ μμ΅λλ€.
// λΆνμνκ² λ³΅μ‘ν μμ²
{
"name": "νκΈΈλ",
"admissionYear": 2024, // λΆνμ
"majorName": "μ»΄ν¨ν°κ³΅ν", // λΆνμ
"doubleMajorName": null // λΆνμ
}
3. μ μ§λ³΄μ 리μ€ν¬
StudentRequestDto
μ μλ‘μ΄ νμ νλκ° μΆκ°λλ©΄?
public class StudentRequestDto {
@NotNull // μλ‘μ΄ νμ νλ μΆκ°
private String phoneNumber;
// κΈ°μ‘΄ νλλ€...
}
β μ΄λ¦λ§ μμ νλ APIκ° μλμΉ μκ² μν₯μ λ°μ μ€λ₯ λ°μ!
β ν΄κ²° λ°©μ: λͺ©μ λ³ DTO λΆλ¦¬
μ€κ³ μμΉ
νλμ APIλ νλμ λͺ νν κ³μ½(Contract)μ κ°μ ΈμΌ ν©λλ€.
βνμ μ΄λ¦ μμ βμ΄λΌλ λ¨μΌ λͺ©μ μ λ§λ μ μ© DTOλ₯Ό μμ±ν©λλ€.
π§ ꡬν κ°μ΄λ
Step 1: μ μ© DTO μμ±
UpdateStudentNameRequestDto.java
(μ κ· μμ±)
package com.kobe.schoolmanagement.dto.request;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor // JSON μμ§λ ¬νλ₯Ό μν κΈ°λ³Έ μμ±μ
public class UpdateStudentNameRequestDto {
@NotBlank(message = "μ΄λ¦μ λΉμλ μ μμ΅λλ€.")
private String name;
}
μ£Όμ νΉμ§
- β
νμν νλλ§ ν¬ν¨ (
name
λ¨μΌ νλ) - β
μ ν¨μ± κ²μ¦ κ·μΉ λͺ
ν (
@NotBlank
) - β λͺ©μ μ΄ λͺ ν (μ΄λ¦ μμ μ μ©)
Step 2: Controller μμ
Before (νμ¬)
@PatchMapping("/change/student/name/{studentId}")
public ResponseEntity<StudentResponseDto> changeStudentName(
@PathVariable String studentId,
@RequestBody StudentRequestDto requestDto // λ²μ© DTO μ¬μ©
) {
return ResponseEntity.ok(studentService.updateStudentName(studentId, requestDto));
}
After (κ°μ )
import com.kobe.schoolmanagement.dto.request.UpdateStudentNameRequestDto;
@PatchMapping("/change/student/name/{studentId}")
public ResponseEntity<StudentResponseDto> changeStudentName(
@PathVariable String studentId,
@RequestBody @Valid UpdateStudentNameRequestDto requestDto // μ μ© DTO + κ²μ¦
) {
return ResponseEntity.ok(studentService.updateStudentName(studentId, requestDto));
}
λ³κ²½ ν¬μΈνΈ
-
StudentRequestDto
βUpdateStudentNameRequestDto
-
@Valid
μ΄λ Έν μ΄μ μΆκ°λ‘ μ ν¨μ± κ²μ¦ νμ±ν
Step 3: Service μμ
Before (νμ¬)
@Transactional
public StudentResponseDto updateStudentName(String studentId, StudentRequestDto requestDto) {
Student findStudent = studentRepository.findByStudentId(studentId)
.orElseThrow(() -> new IllegalArgumentException("μ‘΄μ¬νμ§ μλ νμμ
λλ€."));
findStudent.updateStudentName(requestDto.getName());
return StudentResponseDto.fromEntity(findStudent);
}
After (κ°μ )
import com.kobe.schoolmanagement.dto.request.UpdateStudentNameRequestDto;
@Transactional
public StudentResponseDto updateStudentName(String studentId, UpdateStudentNameRequestDto requestDto) {
Student findStudent = studentRepository.findByStudentId(studentId)
.orElseThrow(() -> new IllegalArgumentException("μ‘΄μ¬νμ§ μλ νμμ
λλ€."));
findStudent.updateStudentName(requestDto.getName());
return StudentResponseDto.fromEntity(findStudent);
}
λ³κ²½ ν¬μΈνΈ
- νλΌλ―Έν° νμ
λ§
UpdateStudentNameRequestDto
λ‘ λ³κ²½ - λλ¨Έμ§ λ‘μ§μ λμΌ
π κ°μ ν¨κ³Ό λΉκ΅
νλͺ© | Before | After |
---|---|---|
API λͺ νμ± | β μ΄λ€ νλκ° νμνμ§ λΆλͺ ν | β nameλ§ νμν¨μ λͺ νν νν |
λ°μ΄ν° μ μ‘ | β λΆνμν νλ ν¬ν¨ κ°λ₯ | β νμν λ°μ΄ν°λ§ μ μ‘ |
μ μ§λ³΄μ | β λ€λ₯Έ API λ³κ²½μ μν₯λ°μ | β λ 립μ μΌλ‘ κ΄λ¦¬ κ°λ₯ |
μ ν¨μ± κ²μ¦ | β οΈ μ묡μ | β
λͺ
μμ (@NotBlank ) |
π‘ API μμ² μμ
κ°μ ν ν΄λΌμ΄μΈνΈ μμ²
{
"name": "νκΈΈλ"
}
μ₯μ
- κ°κ²°νκ³ λͺ νν μμ² κ΅¬μ‘°
- API μλκ° νλμ νμ λ¨
- λΆνμν νλ μ μ‘ μ κ±°
π μ€κ³ μμΉ μ 리
DTO μ€κ³ μ κ³ λ €μ¬ν
-
λ¨μΌ μ±
μ μμΉ
- νλμ DTOλ νλμ λͺ νν λͺ©μ μ κ°μ ΈμΌ ν¨
-
λͺ
μμ κ³μ½
- API μ¬μ©μκ° μ΄λ€ λ°μ΄ν°λ₯Ό 보λ΄μΌ νλμ§ λͺ νν μ μ μμ΄μΌ ν¨
-
λ
립μ±
- λ€λ₯Έ APIμ λ³κ²½μ΄ μν₯μ μ£Όμ§ μλλ‘ λ 립μ μΌλ‘ κ΄λ¦¬
-
μ ν¨μ± κ²μ¦
- DTO λ 벨μμ λͺ μμ μΌλ‘ κ²μ¦ κ·μΉ μ μ
π― κ²°λ‘
λͺ©μ μ λ§λ μ μ© DTOλ₯Ό μ¬μ©νλ©΄:
- API κ³μ½μ΄ λͺ νν΄μ§λλ€
- λ€λ₯Έ κ°λ°μμ νΌλμ λ°©μ§ν©λλ€
- μμ νκ³ μ μ§λ³΄μνκΈ° μ’μ μ½λκ° λ©λλ€
μ΄λ λ¨μν μ½λλ₯Ό λ μ°λ κ²μ΄ μλλΌ, λ λμ μ€κ³λ₯Ό νλ κ²μ λλ€. π