[Spring] CRUD - Controller ๊ณต๋ถ€

2024. 12. 5. 16:07ยท๐Ÿ’š Spring
๋ชฉ์ฐจ
  1. 1. ๊ณ ๊ฐ๊ด€๋ฆฌ CRUD
  2. 1.1 ๊ณ ๊ฐ ๋“ฑ๋ก
  3. 1.2 ๊ณ ๊ฐ ์กฐํšŒ
  4. 1.3 ๊ณ ๊ฐ ์ˆ˜์ •
  5. 1.4 ๊ณ ๊ฐ ์‚ญ์ œ
  6. 1.5 ๊ณ ๊ฐ DTO
  7. 2. ๊ณ„์ขŒ๊ด€๋ฆฌ CRUD
  8. 2.1 ๊ณ„์ขŒ ์ƒ์„ฑ
  9. 2.2 ๊ณ„์ขŒ ์กฐํšŒ
  10. 2.3 ๊ณ„์ขŒ ์ž”์•ก ์กฐํšŒ
  11. 2.4 ๊ณ„์ขŒ ์‚ญ์ œ
  12. 2.5 ๊ณ„์ขŒ DTO
  13. 3. ๊ฑฐ๋ž˜ ๋‚ด์—ญ CRUD
  14. 3.1 ๊ฑฐ๋ž˜ ์กฐํšŒ
  15. 3.2 ๊ฑฐ๋ž˜ ์ƒ์„ฑ
  16. 3.3 ํŠน์ • ๊ฑฐ๋ž˜ ์กฐํšŒ
  17. 3.4 ๊ฑฐ๋ž˜ DTO
728x90
๋ฐ˜์‘ํ˜•
# ์€ํ–‰ ์‹œ์Šคํ…œ API ๊ฐ€์ด๋“œ
## ํ•™์Šต ๋ฐฉ๋ฒ•
1. **์ฃผ์„**
- ์ž‘์„ฑํ•œ ๋ชจ๋“  ์ฝ”๋“œ์— ์ฃผ์„์œผ๋กœ ์„ค๋ช…์„ ์ถ”๊ฐ€
## ํ…Œ์ŠคํŠธ ์ฒดํฌ๋ฆฌ์ŠคํŠธ
- ### ๊ณ ๊ฐ ๊ด€๋ฆฌ API
- [ ] ๋“ฑ๋ก
- [ ] ์กฐํšŒ
- [ ] ์ˆ˜์ •
- [ ] ์‚ญ์ œ
- ### ๊ณ„์ขŒ ๊ด€๋ฆฌ API
- [ ] ์ƒ์„ฑ
- [ ] ์กฐํšŒ
- [ ] ์ž”์•ก ์กฐํšŒ
- [ ] ์‚ญ์ œ
- ### ๊ฑฐ๋ž˜ ๋‚ด์—ญ API
- [ ] ์กฐํšŒ
- [ ] ์ƒ์„ฑ
- [ ] ํŠน์ • ๊ฑฐ๋ž˜ ์กฐํšŒ

Controller์™€ DTD๋งŒ ์‚ฌ์šฉํ•ด์„œ ๋งŒ๋“œ๋Š” CRUD์ด๋‹ค.

Controller์— ์‚ฌ์šฉํ•˜๋Š” Annotation๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ž์„ธํžˆ ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ์ธ ๊ฒƒ ๊ฐ™์•„์„œ, ์ด๋•Œ๋™์•ˆ ๊ตฌํ˜„ํ–ˆ๋˜ ์ฝ”๋“œ๋ฅผ ๋ณด์ง€์•Š๊ณ  ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑ์„ ํ–ˆ๋‹ค.

๋ฐฉ๋ฒ•์€ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฃผ์„๋‹ฌ์•„ ๋ถ„์„ํ•˜๊ธฐ


1. ๊ณ ๊ฐ๊ด€๋ฆฌ CRUD

1.1 ๊ณ ๊ฐ ๋“ฑ๋ก

/** @Controller์™€ @ResponseBody๋ฅผ ๊ฒฐํ•ฉํ•œ ์• ๋„ˆํ…Œ์ด์…˜, ๊ฐ ๋ฉ”์„œ๋“œ๊ฐ€ JSON ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •
* ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” RESTful API ๊ฐœ๋ฐœ ์‹œ ์‚ฌ์šฉ
*/
@RestController
@RequestMapping(("/api/customers"))
// ํด๋ž˜์Šค๋‚˜ ๋ฉ”์„œ๋“œ์— ๊ณตํ†ต๋œ URL ๊ฒฝ๋กœ๋ฅผ ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ,
// "/api/customers" ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ์ด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ฒ˜๋ฆฌ ํ•˜๋„๋ก ์„ค์ •
public class CustomerController {
// ๊ณ ๊ฐ ๋“ฑ๋ก
@PostMapping
/**
* @PostMapping : HTTP Post Method์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์ถ• ํ‘œํ˜„์œผ๋กœ, ์ฃผ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ์„œ๋ฒ„์— ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉ
* ResponseEntity : HTTP Response ์ƒํƒœ ์ฝ”๋“œ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ์„ค์ • ๋ฐ ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅ
* @RequestBody : ์š”์ฒญ์˜ JSON ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ”์ธ๋”ฉ
* CustomerRequestDto customerRequestDto : ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š” ๊ฐ์ฒด, ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ ๋ฐ›์Œ
* ResponseEntity<CustomerResponseDto> : ์‘๋‹ต ๋ณธ๋ฌธ์— CustomerResponseDto ๊ฐ์ฒด๋ฅผ ๋‹ด์•„ ๋ฐ˜ํ™˜
* */
public ResponseEntity<CustomerResponseDto> createCustomers(@RequestBody CustomerRequestDto customerRequestDto){
// ์š”์ฒญ ์ฒ˜๋ฆฌ ๋กœ์ง
// Controller ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„ ์ž„์œผ๋กœ API ๋ช…์„ธ์„œ (API.md)์—์„œ ์š”๊ตฌํ•œ Response ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜
// id๋Š” ์ž„์‹œ๋กœ ๊ฐ’ ์ง€์ •, ๊ทธ ์™ธ์˜ ์š”์ฒญ ๋ฐ์ดํ„ฐ๋Š” response(์‘๋‹ต) Dto์— ์ง์ ‘์ ์œผ๋กœ ๊ฐ’ ๋„ฃ์–ด์คŒ
CustomerResponseDto customerResponseDto = new CustomerResponseDto(
1L, // id
customerRequestDto.getName(), // customerRequestDto์—์„œ ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜จ๋‹ค
customerRequestDto.getEmail(), // customerRequestDto์—์„œ ์ด๋ฉ”์ผ์„ ๊ฐ€์ ธ์˜จ๋‹ค
customerRequestDto.getPhone(), // customerRequestDto์—์„œ ํฐ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
customerRequestDto.getAddress() // customerRequestDto์—์„œ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
);
// ResponseEntity
// ์ƒํƒœ์ฝ”๋“œ CREATED(201)์™€ ์‘๋‹ต๋ฐ์ดํ„ฐ(customerResponseDto)๋ฅผ JSONํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
return ResponseEntity.status(HttpStatus.CREATED).body(customerResponseDto);
}

 

1.2 ๊ณ ๊ฐ ์กฐํšŒ

// ๊ณ ๊ฐ ์กฐํšŒ
/**
* @GetMapping("/{customerId}") : HTTP GET ๋ฉ”์„œ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, "/{customerId}" ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ํŠน์ • ๊ณ ๊ฐ ID๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งคํ•‘
* @PathVariable : URL ๊ฒฝ๋กœ์˜ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฉ”์„œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ
* ResponseEntity<CustomerResponseDto> : ์‘๋‹ต ๋ณธ๋ฌธ์— CustomerResponseDto ๊ฐ์ฒด๋ฅผ ๋‹ด์•„ ๋ฐ˜ํ™˜
*/
@GetMapping("/{customerId}")
public ResponseEntity<CustomerResponseDto> searchCustomers(@PathVariable Long customerId){
// CustomerResponseDto ๊ฐ์ฒด๋ฅผ ์•„๋ž˜์˜ ๊ฐ’๋“ค์„ ์ž…๋ ฅํ•œ ํ›„ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค
CustomerResponseDto customerResponseDto = new CustomerResponseDto(
customerId, // ๊ณ ๊ฐ Id
"Robbie", // ์ž„์‹œ ์ด๋ฆ„
"Robbie@example.com", // ์ž„์‹œ ์ด๋ฉ”์ผ
"010-1234-5678", // ์ž„์‹œ ์ „ํ™”๋ฒˆํ˜ธ
"123 Seoul"// ์ž„์‹œ ์ฃผ์†Œ
);
// ์ƒํƒœ์ฝ”๋“œ OK๋กœ ๋ฐ˜ํ™˜, ๊ฐ’์„ ์ž…๋ ฅํ•œ customerResponseDto ๊ฐ์ฒด๋ฅผ JSON ํ˜•์‹์œผ๋กœ BODY์— ์ถœ๋ ฅ
return ResponseEntity.status(HttpStatus.OK).body(customerResponseDto);
}

 

1.3 ๊ณ ๊ฐ ์ˆ˜์ •

// ๊ณ ๊ฐ ์ •๋ณด ์ˆ˜์ •
/**
* HTTP Put Method์— ํ•ด๋‹น, ์„œ๋ฒ„์— ๋ฆฌ์†Œ์Šค๋ฅผ ์ „์ฒด ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉ
* URL์— /{customerId} ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, ํŠน์ • ๊ณ ๊ฐ์˜ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•˜๋„๋ก ๋งตํ•‘ ๋˜์–ด์žˆ์Œ
* */
@PutMapping("/{customerId}")
/**
* @PathVariable : URL ๊ฒฝ๋กœ์— ํฌํ•จ๋œ ๋ณ€์ˆ˜(customerId)๋ฅผ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ
* ResponseEntity<CustomerResponseDto> : ์ˆ˜์ •๋œ ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ์‘๋‹ต ๋ณธ๋ฌธ์œผ๋กœ ๋ฐ˜ํ™˜
*/
public ResponseEntity<CustomerResponseDto> updateCustomers(@PathVariable Long customerId){
// ์ˆ˜์ • ๊ฐ’์„ ์‘๋‹ต ๋ฐ›์„, ์‘๋‹ต ๋ฐ์ดํ„ฐ ๊ฐ์ฒด ์ƒ์„ฑ
CustomerResponseDto customerResponseDto = new CustomerResponseDto(
customerId,
"ํ•˜์ง„์ด", // ์ˆ˜์ •ํ•  ์ด๋ฆ„
"wlsdl7520@naver.com", // ์ˆ˜์ •ํ•  ์ด๋ฉ”์ผ
"010-1234-5678", // ์ˆ˜์ •ํ•  ์ „ํ™”๋ฒˆํ˜ธ
"Daegu" // ์ˆ˜์ •ํ•  ์ฃผ์†Œ
);
// ์ƒํƒœ์ฝ”๋“œ OK(200)์™€ ์ˆ˜์ •๋œ ๋ฐ์ดํ„ฐ(customerResponseDto) ๊ฐ์ฒด๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
return ResponseEntity.status(HttpStatus.OK).body(customerResponseDto);
}

 

1.4 ๊ณ ๊ฐ ์‚ญ์ œ

// ๊ณ ๊ฐ ์‚ญ์ œ
/**
* HTTP DELETE ๋ฉ”์„œ๋“œ๋กœ, ์„œ๋ฒ„์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉ
* URL ๊ฒฝ๋กœ์— "/{customerId}"๋ฅผ ํฌํ•จํ•˜์—ฌ ํŠน์ • ๊ณ ๊ฐ์„ ์‚ญ์ œํ•˜๋„๋ก ๋งคํ•‘
* */
@DeleteMapping("/{customerId}")
/**
* @PathVariable : URL ๊ฒฝ๋กœ์— ํฌํ•จ๋œ ๋ณ€์ˆ˜(customerId)๋ฅผ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ
* ResponseEntity<String> : ์‘๋‹ต ๋ณธ๋ฌธ์œผ๋กœ String ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
* */
public ResponseEntity<String> deleteCustomers(@PathVariable Long customerId){
// ์ƒํƒœ์ฝ”๋“œ OK(200)๊ณผ ์‚ญ์ œ ์„ฑ๊ณต ๋ฉ”์„ธ์ง€("์‚ญ์ œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค")๋ฅผ ๋ฐ˜ใ…Žํ™˜
return ResponseEntity.status(HttpStatus.OK).body("์‚ญ์ œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค");
}

 

1.5 ๊ณ ๊ฐ DTO

// ๊ฐ์ฒด์˜ ์บก์Аํ™”๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ, ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ
// @Getter ์‚ฌ์šฉ ์‹œ, get๋ฉ”์„œ๋“œ ์ž๋™์ƒ์„ฑ
@Getter
public class CustomerRequestDto {
private String name; // ์ด๋ฆ„
private String email; // ์ด๋ฉ”์ผ
private String phone; // ํฐ๋ฒˆํ˜ธ
private String address; // ์ฃผ์†Œ
// ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์™ธ๋ถ€์—์„œ ๋ฐ›์€ ๊ฐ’์„ ํ•„๋“œ์— ๋„ฃ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
// ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ํ•œ ๋ฒˆ์— ๋ฐ›์•„์„œ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ
public CustomerRequestDto(String name, String email, String phone, String address) {
this.name = name;
this.email = email;
this.phone = phone;
this.address = address;
}
}
// ๊ฐ์ฒด์˜ ์บก์Аํ™”๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ, ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ
// @Getter ์‚ฌ์šฉ ์‹œ, get๋ฉ”์„œ๋“œ ์ž๋™์ƒ์„ฑ
@Getter
public class CustomerResponseDto {
@Id // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ Primary Key์ž„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค
/**
* GenerationType : ์ž๋™ ์ฆ๊ฐ€ ๋ฐฉ์‹์„ ์„ค์ •ํ•˜๋Š” ์˜ต์…˜
* GenerationType.AUTO : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ Auto Increment ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉ, JPA๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋งž๋Š” ๋ฐฉ์‹์„ ์ž๋™์œผ๋กœ ์„ ํƒ
* */
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id; // id๊ฐ’
private final String name; // ์ด๋ฆ„
private final String email; // ์ด๋ฉ”์ผ
private final String phone; // ํฐ๋ฒˆํ˜ธ
private final String address; // ์ฃผ์†Œ
// ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์™ธ๋ถ€์—์„œ ๋ฐ›์€ ๊ฐ’์„ ํ•„๋“œ์— ๋„ฃ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
// ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ํ•œ ๋ฒˆ์— ๋ฐ›์•„์„œ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ
public CustomerResponseDto(Long id, String name, String email, String phone, String address) {
this.id = id;
this.name = name;
this.email = email;
this.phone = phone;
this.address = address;
}
}

 


2. ๊ณ„์ขŒ๊ด€๋ฆฌ CRUD

2.1 ๊ณ„์ขŒ ์ƒ์„ฑ

/** @Controller์™€ @ResponseBody๋ฅผ ๊ฒฐํ•ฉํ•œ ์• ๋„ˆํ…Œ์ด์…˜, ๊ฐ ๋ฉ”์„œ๋“œ๊ฐ€ JSON ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •
* ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” RESTful API ๊ฐœ๋ฐœ ์‹œ ์‚ฌ์šฉ
*/
@RestController
@RequestMapping("/api/accounts")
// ํด๋ž˜์Šค๋‚˜ ๋ฉ”์„œ๋“œ์— ๊ณตํ†ต๋œ URL ๊ฒฝ๋กœ๋ฅผ ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ,
// "/api/accounts" ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ์ด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ฒ˜๋ฆฌ ํ•˜๋„๋ก ์„ค์ •
public class AccountController {
// ๊ณ„์ขŒ ์ƒ์„ฑ
@PostMapping
/**
* @PostMapping : HTTP Post Method์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์ถ• ํ‘œํ˜„์œผ๋กœ, ์ฃผ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ์„œ๋ฒ„์— ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉ
* ResponseEntity : HTTP Response ์ƒํƒœ ์ฝ”๋“œ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ์„ค์ • ๋ฐ ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅ
* @RequestBody : ์š”์ฒญ์˜ JSON ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ”์ธ๋”ฉ
* AccountResponseDto accountRequestDto : ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š” ๊ฐ์ฒด, ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ ๋ฐ›์Œ
* ResponseEntity<AccountResponseDto> : ์‘๋‹ต ๋ณธ๋ฌธ์— AccountResponseDto ๊ฐ์ฒด๋ฅผ ๋‹ด์•„ ๋ฐ˜ํ™˜
* */
public ResponseEntity<AccountResponseDto> createAccount(@RequestBody AccountRequestDto accountRequestDto){
// ์š”์ฒญ ์ฒ˜๋ฆฌ ๋กœ์ง
// Controller ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„ ์ž„์œผ๋กœ API ๋ช…์„ธ์„œ (API.md)์—์„œ ์š”๊ตฌํ•œ Response ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜
// id๋Š” ์ž„์‹œ๋กœ ๊ฐ’ ์ง€์ •, ๊ทธ ์™ธ์˜ ์š”์ฒญ ๋ฐ์ดํ„ฐ๋Š” response(์‘๋‹ต) Dto์— ์ง์ ‘์ ์œผ๋กœ ๊ฐ’ ๋„ฃ์–ด์คŒ
AccountResponseDto accountResponseDto = new AccountResponseDto(
101L,
accountRequestDto.getCustomerId(),
accountRequestDto.getAccountType(),
accountRequestDto.getInitialDeposit()
);
// ์ƒํƒœ์ฝ”๋“œ CREATED(201)์™€ ์‘๋‹ต๋ฐ์ดํ„ฐ(customerResponseDto)๋ฅผ JSONํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
return ResponseEntity.status(HttpStatus.CREATED).body(accountResponseDto);
}

 

2.2 ๊ณ„์ขŒ ์กฐํšŒ

// ๊ณ„์ขŒ ์กฐํšŒ
/**
* @GetMapping("/{accountId}") : HTTP GET ๋ฉ”์„œ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, "/{accountId}" ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ํŠน์ • ๊ณ ๊ฐ ID๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งคํ•‘
* @PathVariable : URL ๊ฒฝ๋กœ์˜ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฉ”์„œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ
* ResponseEntity<AccountResponseDto> : ์‘๋‹ต ๋ณธ๋ฌธ์— AccountResponseDto ๊ฐ์ฒด๋ฅผ ๋‹ด์•„ ๋ฐ˜ํ™˜
*/
@GetMapping("/{accountId}")
public ResponseEntity<AccountResponseDto> searchAccount(@PathVariable Long accountId){
// AccountResponseDto ๊ฐ์ฒด๋ฅผ ์•„๋ž˜์˜ ๊ฐ’๋“ค์„ ์ž…๋ ฅํ•œ ํ›„ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค
AccountResponseDto accountResponseDto = new AccountResponseDto(
accountId,
1,
"SAVINGS",
1000.0
);
// ์ƒํƒœ์ฝ”๋“œ OK๋กœ ๋ฐ˜ํ™˜, ๊ฐ’์„ ์ž…๋ ฅํ•œ customerResponseDto ๊ฐ์ฒด๋ฅผ JSON ํ˜•์‹์œผ๋กœ BODY์— ์ถœ๋ ฅ
return ResponseEntity.status(HttpStatus.OK).body(accountResponseDto);
}

 

2.3 ๊ณ„์ขŒ ์ž”์•ก ์กฐํšŒ

// ๊ณ„์ขŒ ์ž”์•ก ์กฐํšŒ
/**
* @GetMapping("/{accountId}/balance") : HTTP GET ๋ฉ”์„œ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, "/{accountId}/balance" ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ํŠน์ • ๊ณ ๊ฐ ID๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งคํ•‘
* @PathVariable : URL ๊ฒฝ๋กœ์˜ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฉ”์„œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ
* ResponseEntity<AccountBalaceResponseDto> : ์‘๋‹ต ๋ณธ๋ฌธ์— AccountBalaceResponseDto ๊ฐ์ฒด๋ฅผ ๋‹ด์•„ ๋ฐ˜ํ™˜
*/
@GetMapping("/{accountId}/balance")
public ResponseEntity<AccountBalaceResponseDto> searchBalanceAccount(@PathVariable Long accountId){
// CustomerResponseDto ๊ฐ์ฒด๋ฅผ ์•„๋ž˜์˜ ๊ฐ’๋“ค์„ ์ž…๋ ฅํ•œ ํ›„ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค
AccountBalaceResponseDto accountBalaceResponseDto = new AccountBalaceResponseDto(
accountId,
1000.0
);
// ์ƒํƒœ์ฝ”๋“œ OK(200)์™€ ์ˆ˜์ •๋œ ๋ฐ์ดํ„ฐ(customerResponseDto) ๊ฐ์ฒด๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
return ResponseEntity.status(HttpStatus.OK).body(accountBalaceResponseDto);
}

 

2.4 ๊ณ„์ขŒ ์‚ญ์ œ

// ๊ณ„์ขŒ ์‚ญ์ œ
/**
* HTTP DELETE ๋ฉ”์„œ๋“œ๋กœ, ์„œ๋ฒ„์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉ
* URL ๊ฒฝ๋กœ์— "/{accountId}"๋ฅผ ํฌํ•จํ•˜์—ฌ ํŠน์ • ๊ณ ๊ฐ์„ ์‚ญ์ œํ•˜๋„๋ก ๋งคํ•‘
* */
@DeleteMapping("/{accountId}")
/**
* @PathVariable : URL ๊ฒฝ๋กœ์— ํฌํ•จ๋œ ๋ณ€์ˆ˜(accountId)๋ฅผ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ
* ResponseEntity<String> : ์‘๋‹ต ๋ณธ๋ฌธ์œผ๋กœ String ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
* */
public ResponseEntity<String> deleteAccount(@PathVariable Long accountId){
// ์ƒํƒœ์ฝ”๋“œ OK(200)๊ณผ ์‚ญ์ œ ์„ฑ๊ณต ๋ฉ”์„ธ์ง€("์‚ญ์ œ ์ •์ƒ์ ์œผ๋กœ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค")๋ฅผ ๋ฐ˜ํ™˜
return ResponseEntity.status(HttpStatus.OK).body("์‚ญ์ œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
}

 

2.5 ๊ณ„์ขŒ DTO

// ๊ฐ์ฒด์˜ ์บก์Аํ™”๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ, ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ
// @Getter ์‚ฌ์šฉ ์‹œ, get๋ฉ”์„œ๋“œ ์ž๋™์ƒ์„ฑ
@Getter
public class AccountRequestDto {
private Integer customerId;
private String accountType;
private Double initialDeposit;
// ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์™ธ๋ถ€์—์„œ ๋ฐ›์€ ๊ฐ’์„ ํ•„๋“œ์— ๋„ฃ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
// ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ํ•œ ๋ฒˆ์— ๋ฐ›์•„์„œ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ
public AccountRequestDto(Integer customerId, String accountType, Double initialDeposit) {
this.customerId = customerId;
this.accountType = accountType;
this.initialDeposit = initialDeposit;
}
}
// ๊ฐ์ฒด์˜ ์บก์Аํ™”๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ, ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ
// @Getter ์‚ฌ์šฉ ์‹œ, get๋ฉ”์„œ๋“œ ์ž๋™์ƒ์„ฑ
@Getter
public class AccountResponseDto {
private Long accountId;
private Integer customerId;
private String accountType;
private Double balance;
// ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์™ธ๋ถ€์—์„œ ๋ฐ›์€ ๊ฐ’์„ ํ•„๋“œ์— ๋„ฃ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
// ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ํ•œ ๋ฒˆ์— ๋ฐ›์•„์„œ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ
public AccountResponseDto(Long accountId, Integer customerId, String accountType, Double balance) {
this.accountId = accountId;
this.customerId = customerId;
this.accountType = accountType;
this.balance = balance;
}
}
// ๊ฐ์ฒด์˜ ์บก์Аํ™”๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ, ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ
// @Getter ์‚ฌ์šฉ ์‹œ, get๋ฉ”์„œ๋“œ ์ž๋™์ƒ์„ฑ
@Getter
public class AccountBalaceResponseDto {
private Long accountId;
private Double balance;
// ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์™ธ๋ถ€์—์„œ ๋ฐ›์€ ๊ฐ’์„ ํ•„๋“œ์— ๋„ฃ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
// ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ํ•œ ๋ฒˆ์— ๋ฐ›์•„์„œ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ
public AccountBalaceResponseDto(Long accountId, Double balance) {
this.accountId = accountId;
this.balance = balance;
}
}

3. ๊ฑฐ๋ž˜ ๋‚ด์—ญ CRUD

3.1 ๊ฑฐ๋ž˜ ์กฐํšŒ

/** @Controller์™€ @ResponseBody๋ฅผ ๊ฒฐํ•ฉํ•œ ์• ๋„ˆํ…Œ์ด์…˜, ๊ฐ ๋ฉ”์„œ๋“œ๊ฐ€ JSON ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •
* ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” RESTful API ๊ฐœ๋ฐœ ์‹œ ์‚ฌ์šฉ
*/
@RestController
@RequestMapping("/api/transactions")
// ํด๋ž˜์Šค๋‚˜ ๋ฉ”์„œ๋“œ์— ๊ณตํ†ต๋œ URL ๊ฒฝ๋กœ๋ฅผ ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ,
// "/api/transactions" ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ์ด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ฒ˜๋ฆฌ ํ•˜๋„๋ก ์„ค์ •
public class TransactionController {
// ๊ฑฐ๋ž˜ ๋‚ด์—ญ ์กฐํšŒ
// @GetMapping : HTTP GET ๋ฉ”์„œ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ์ผ๋ฐ˜์ ์ธ ์š”์ฒญ ์ „์ฒด๋ฅผ ์กฐํšŒ
@GetMapping
// ResponseEntity : HTTP Response ์ƒํƒœ ์ฝ”๋“œ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ์„ค์ • ๋ฐ ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅ
public ResponseEntity<List<TransactionResponseDto>> getTransaction(@ModelAttribute TransactionRequestDto requestDto){
// ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ฑฐ๋ž˜ ๋‚ด์—ญ(List<TransactionResponseDto>)์„ ์‘๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜
// @ModelAttribute : HTTP ์š”์ฒญ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ TransactionRequestDto ๊ฐ์ฒด์— ์ž๋™์œผ๋กœ ๋งคํ•‘
// - ์š”์ฒญ URL์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ TransactionRequestDto ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ
// - ์˜ˆ) ์š”์ฒญ URL: /transactions?customerId=101&transactionType=DEPOSIT
// -> requestDto.customerId = 101
// -> requestDto.transactionType = "DEPOSIT"
// ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฑฐ๋ž˜๋“ค์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด List ์‚ฌ์šฉํ•ด์„œ, ๊ฐ์ฒด ์ƒ์„ฑ
List<TransactionResponseDto> transaction = new ArrayList<>();
// ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ TransactionResponseDto ๊ฐ์ฒด์— ๋‹ด์•„ ์ƒˆ๋กœ ์ƒ์„ฑ ํ›„ transaction ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
transaction.add(new TransactionResponseDto(1001L,101,"DEPOSIT",500.0,"2024-01-15T10:00:00"));
transaction.add(new TransactionResponseDto(1002L,101,"WITHDRAWAL",200.0,"2024-01-20T14:30:00"));
// ์ƒˆ๋กœ์šด ์‘๋‹ต์ฝ”๋“œ OK(200)์„ ๋ฐ˜ํ™˜
return new ResponseEntity<>(transaction,HttpStatus.OK);
}

 

3.2 ๊ฑฐ๋ž˜ ์ƒ์„ฑ

// ๊ฑฐ๋ž˜ ์ƒ์„ฑ
@PostMapping
/**
* @PostMapping : HTTP Post Method์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์ถ• ํ‘œํ˜„์œผ๋กœ, ์ฃผ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ์„œ๋ฒ„์— ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉ
* ResponseEntity : HTTP Response ์ƒํƒœ ์ฝ”๋“œ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ์„ค์ • ๋ฐ ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅ
* @RequestBody : ์š”์ฒญ์˜ JSON ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ”์ธ๋”ฉ
* TransactionResponseDto transactionResponseDto : ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š” ๊ฐ์ฒด, ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ ๋ฐ›์Œ
* ResponseEntity<TransactionResponseDto> : ์‘๋‹ต ๋ณธ๋ฌธ์— transactionResponseDto ๊ฐ์ฒด๋ฅผ ๋‹ด์•„ ๋ฐ˜ํ™˜
* */
public ResponseEntity<TransactionResponseDto> createTransaction(@RequestBody TransactionRequestDto requestDto){
// ์š”์ฒญ ์ฒ˜๋ฆฌ ๋กœ์ง
// ํ˜„์žฌ ๋‹จ๊ณ„๋Š” Controller ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋‹จ๊ณ„์ด๋ฏ€๋กœ, API ๋ช…์„ธ์„œ(API.md)์— ์ •์˜๋œ ํ˜•์‹์œผ๋กœ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜
// id๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™์ด ์—†์œผ๋ฏ€๋กœ ์ž„์‹œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•˜๊ณ ,
// ๋‚˜๋จธ์ง€ ํ•„๋“œ๋Š” ์š”์ฒญ(Request) DTO์—์„œ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉ
TransactionResponseDto transactionResponseDto = new TransactionResponseDto(
// ์ž„์‹œ๋กœ ๊ฐ’์„ ๋„ฃ์–ด์คŒ
1003L,
101,
"DEPOSIT",
1000.0,
"2024-02-01T11:00:00"
);
// ์ƒํƒœ์ฝ”๋“œ OK(200)์™€ ์‘๋‹ต๋ฐ์ดํ„ฐ(TransactionResponseDto)๋ฅผ JSONํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
return ResponseEntity.status(HttpStatus.OK).body(transactionResponseDto);
}

 

3.3 ํŠน์ • ๊ฑฐ๋ž˜ ์กฐํšŒ

// ํŠน์ • ๊ฑฐ๋ž˜ ์กฐํšŒ
/**
* @GetMapping("/{transactionId}") : HTTP GET ๋ฉ”์„œ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, "/{transactionId}" ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ํŠน์ • ๊ณ ๊ฐ ID๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งคํ•‘
* @PathVariable : URL ๊ฒฝ๋กœ์˜ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฉ”์„œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ
* ResponseEntity<TransactionResponseDto> : ์‘๋‹ต ๋ณธ๋ฌธ์— TransactionResponseDto ๊ฐ์ฒด๋ฅผ ๋‹ด์•„ ๋ฐ˜ํ™˜
*/
@GetMapping("/{transactionId}")
public ResponseEntity<TransactionResponseDto> findTransaction(@PathVariable Long transactionId){
// ์š”์ฒญ ์ฒ˜๋ฆฌ ๋กœ์ง
// ํ˜„์žฌ ๋‹จ๊ณ„๋Š” Controller ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋‹จ๊ณ„์ด๋ฏ€๋กœ, API ๋ช…์„ธ์„œ(API.md)์— ์ •์˜๋œ ํ˜•์‹์œผ๋กœ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜
// id๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™์ด ์—†์œผ๋ฏ€๋กœ ์ž„์‹œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•˜๊ณ ,
// ๋‚˜๋จธ์ง€ ํ•„๋“œ๋Š” ์š”์ฒญ(Request) DTO์—์„œ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉ
TransactionResponseDto transactionResponseDto = new TransactionResponseDto(
transactionId,
101,
"DEPOSIT",
500.0,
"2024-01-15T10:00:00"
);
// ์ƒํƒœ์ฝ”๋“œ OK(200)์™€ ์‘๋‹ต๋ฐ์ดํ„ฐ(TransactionResponseDto)๋ฅผ JSONํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
return ResponseEntity.status(HttpStatus.OK).body(transactionResponseDto);
}

 

3.4 ๊ฑฐ๋ž˜ DTO

// ๊ฐ์ฒด์˜ ์บก์Аํ™”๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ, ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ
// @Getter ์‚ฌ์šฉ ์‹œ, get๋ฉ”์„œ๋“œ ์ž๋™์ƒ์„ฑ
@Getter
public class TransactionRequestDto {
private Integer customerId;
private String startDate;
private String endDate;
// ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์™ธ๋ถ€์—์„œ ๋ฐ›์€ ๊ฐ’์„ ํ•„๋“œ์— ๋„ฃ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
// ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ํ•œ ๋ฒˆ์— ๋ฐ›์•„์„œ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ
public TransactionRequestDto(Integer customerId, String startDate, String endDate) {
this.customerId = customerId;
this.startDate = startDate;
this.endDate = endDate;
}
}
// ๊ฐ์ฒด์˜ ์บก์Аํ™”๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ, ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด์˜ ํ•„๋“œ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ
// @Getter ์‚ฌ์šฉ ์‹œ, get๋ฉ”์„œ๋“œ ์ž๋™์ƒ์„ฑ
@Getter
public class TransactionResponseDto {
private Long transactionId;
private Integer accountId;
private String transactionType;
private Double amount;
private String transactionDate;
// ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์™ธ๋ถ€์—์„œ ๋ฐ›์€ ๊ฐ’์„ ํ•„๋“œ์— ๋„ฃ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
// ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ํ•œ ๋ฒˆ์— ๋ฐ›์•„์„œ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ
public TransactionResponseDto(Long transactionId, Integer accountId,
String transactionType, Double amount, String transactionDate) {
this.transactionId = transactionId;
this.accountId = accountId;
this.transactionType = transactionType;
this.amount = amount;
this.transactionDate = transactionDate;
}
}

 

728x90
๋ฐ˜์‘ํ˜•
์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'๐Ÿ’š Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Spring] ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ(Annotation ์‚ฌ์šฉX)  (0) 2024.12.12
[Spring] MVC ํŒจํ„ด์ด๋ž€?  (0) 2024.12.06
[Spring] Spring MVC ๊ตฌ์กฐ  (0) 2024.12.02
[Spring] Spring Annotation 1  (0) 2024.11.18
[Spring] .toEntity() .toList() .toString()์ด๋ž€?  (0) 2024.11.05
  1. 1. ๊ณ ๊ฐ๊ด€๋ฆฌ CRUD
  2. 1.1 ๊ณ ๊ฐ ๋“ฑ๋ก
  3. 1.2 ๊ณ ๊ฐ ์กฐํšŒ
  4. 1.3 ๊ณ ๊ฐ ์ˆ˜์ •
  5. 1.4 ๊ณ ๊ฐ ์‚ญ์ œ
  6. 1.5 ๊ณ ๊ฐ DTO
  7. 2. ๊ณ„์ขŒ๊ด€๋ฆฌ CRUD
  8. 2.1 ๊ณ„์ขŒ ์ƒ์„ฑ
  9. 2.2 ๊ณ„์ขŒ ์กฐํšŒ
  10. 2.3 ๊ณ„์ขŒ ์ž”์•ก ์กฐํšŒ
  11. 2.4 ๊ณ„์ขŒ ์‚ญ์ œ
  12. 2.5 ๊ณ„์ขŒ DTO
  13. 3. ๊ฑฐ๋ž˜ ๋‚ด์—ญ CRUD
  14. 3.1 ๊ฑฐ๋ž˜ ์กฐํšŒ
  15. 3.2 ๊ฑฐ๋ž˜ ์ƒ์„ฑ
  16. 3.3 ํŠน์ • ๊ฑฐ๋ž˜ ์กฐํšŒ
  17. 3.4 ๊ฑฐ๋ž˜ DTO
'๐Ÿ’š Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [Spring] ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ(Annotation ์‚ฌ์šฉX)
  • [Spring] MVC ํŒจํ„ด์ด๋ž€?
  • [Spring] Spring MVC ๊ตฌ์กฐ
  • [Spring] Spring Annotation 1
Genie_.
Genie_.
๋‚ด๊ฐ€ ๊ณต๋ถ€ํ•˜๋Š” ์ง€์‹์„ ๋ณต์Šตํ•˜๊ณ  ๊ธฐ๋กํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค ๐Ÿ’ฌ
  • Genie_.
    Geine Dev
    Genie_.
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ์ „์ฒด (153)
      • ๐Ÿ•น๏ธ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ (37)
        • ์ฝ”๋“œํŠธ๋ฆฌ (13)
        • ๋ฐฑ์ค€ (17)
        • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (7)
      • ๐Ÿงก JavaScript (9)
      • ๐Ÿ’™ React (0)
      • ๐Ÿ’œ HTML,CSS (38)
      • ๐Ÿ’š Spring (6)
      • โค๏ธ JAVA (13)
        • Basic (13)
      • ๐ŸŒŸ CS (1)
      • โ„๏ธ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ 7๊ธฐ (41)
      • ๐Ÿ… ์ž๊ฒฉ์ฆ (6)
        • ์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ (0)
        • SQLD (3)
        • ์ปดํ“จํ„ฐํ™œ์šฉ๋Šฅ๋ ฅ2๊ธ‰ (3)
      • ๐Ÿ—จ GIT (1)
      • ๐ŸŒฐ ์Šคํ„ฐ๋”” (1)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ๊ธ€์“ฐ๊ธฐ
  • ๊ณต์ง€์‚ฌํ•ญ

    • ๋ฉ”๋ชจ
  • ๋ฐ˜์‘ํ˜•
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Genie_.
[Spring] CRUD - Controller ๊ณต๋ถ€

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.