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 |