JDBC-2 ์ถ๊ฐ๊ตฌํ +@Builder
Categories: Spring
Tags: Spring, ๊ฐ๋ ์ ๋ฆฌ
๐ ๊ฐ์ธ์ ์ธ ๊ณต๊ฐ์ผ๋ก ๊ณต๋ถ๋ฅผ ๊ธฐ๋กํ๊ณ ๋ณต์ตํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ธ๋ก๊ทธ์
๋๋ค.
์ ํํ์ง ์์ ์ ๋ณด๊ฐ ์์ ์ ์์ผ๋ ์ฐธ๊ณ ๋ฐ๋๋๋ค :๐ธ
[ํ๋ฆฐ ๋ด์ฉ์ ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์๋ฉด ๋ณต๋ฐ์ผ์ค๊ฑฐ์์]
DTO ์์ ํ๊ธฐ
๋ณด์ํ๊ธฐ โ URI๋ฅผ ํค๋์ ์ถ๊ฐํด์ ์ ๋ฌ / ์ฌ๋ฌ ์์ ์ปคํผ ์ฃผ๋ฌธ๋ ์๋ตํ ์ ์๊ฒ ํ๊ธฐ!
POST
- ํด๋ผ์ด์ธํธ ์ธก์์ ์ด๋ค ๋ฆฌ์์ค์ ๋ฑ๋ก์ ์์ฒญํ ๊ฒฝ์ฐ , ํด๋น ๋ฆฌ์์ค๋ฅผ DB์ ์ ์ฅํ ํ ์์ฒญํ ๋ฆฌ์์ค๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ ์ฅ๋์์์ 201 Created HTTP Status๋ฅผ response header์ ์ถ๊ฐํด์ ํด๋ผ์ด์ธํธ ์ธก์ ์๋ต์ผ๋ก ์ ๋ฌ
-
๊ฑฐ๊ธฐ์ ๋ํด DB์ ์ ์ฅ๋ ๋ฆฌ์์ค์ ์์น๋ฅผ ์๋ ค์ฃผ๋ ์์น ์ ๋ณด(Location)์ธ URI๋ฅผ ์ญ์ response header์ ์ถ๊ฐํด์ ์๋ต์ผ๋ก ์ ๋ฌ
- ์ฐธ๊ณ ๋ก โญ ๋ฐฑ์๋ ์ ํ๋ฆฌ์ผ์ด์ ์ธก์ ๋ฆฌ์์ค๋ฅผ ๋ฑ๋กํ ๊ฒฝ์ฐ์๋ ๋ฑ๋ก๋ ๋ฆฌ์์ค์ ์ ๋ณด๋ฅผ ์๋ต์ผ๋ก ๋ฆฌํดํ ํ์๊ฐ ์์
์ฌ๋ฌ ์์ ์ปคํผ ์ฃผ๋ฌธ๊ณผ ์๋ต ๊ฐ๋ฅํ๊ฒ ํ๊ธฐ
-
์ฌ๋ฌ ์์ ์ปคํผ๋ฅผ ์ฃผ๋ฌธํ ์ ์๊ฒ ํ๊ธฐ - OrderPostDto ์์
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//๊ธฐ์กด ์ฝ๋ package com.springboot.order.dto; import javax.validation.constraints.Positive; @Getter @Setter public class OrderPostDto { @Positive private long memberId; @Positive private long coffeeId; //๋ณ๊ฒฝํ ๋ถ๋ถ }
-
์ฌ๋ฌ ์์ ์ปคํผ๋ฅผ ์ฃผ๋ฌธํ ์ ์๋๋ก ์์ ํด์ผ ํจ.
1 2
@Valid private List<OrderCoffeeDto> ordercoffees;
-
-
์ฌ๋ฌ ์์ ์ปคํผ๋ฅผ ์ฃผ๋ฌธํ๊ธฐ ์ํด ์ถ๊ฐ - OrderCoffeeDto
1 2 3 4 5 6 7 8 9 10
@Getter @AllArgsConstructor public class OrderCoffeeDto { @Positive private long coffeeId; @Positive private int quantity; }
-
์ฌ๋ฌ ์์ ์ปคํผ ์ ๋ณด๋ฅผ ์๋ต์ผ๋ก ์ ๊ณตํ๊ธฐ ์ํด ์ถ๊ฐ - OrderCoffeeResponseDto
1 2 3 4 5 6 7 8 9 10
@Getter @AllArgsConstructor public class OrderCoffeeResponseDto { private long coffeeId; private String korName; private String engName; private int price; private int quantity; }
-
์ฃผ๋ฌธํ ์ฌ๋ฌ ๊ฑด์ ์ปคํผ ์ ๋ณด๋ฅผ ์๋ตํ๊ธฐ ์ํด ์์ - OrderResponseDto
1 2 3 4 5 6 7 8 9 10 11 12 13
package com.springboot.order.dto; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; @Getter @Setter @AllArgsConstructor public class OrderResponseDto { private long memberId; private long coffeeId; }
-
์ฌ๋ฌ ์์ ์ปคํผ ์ ๋ณด๋ฅผ ์๋ต์ผ๋ก ์ ์กํ ์ ์๊ฒ ๋ณ๊ฒฝ + ์ฃผ๋ฌธ ์๊ฐ๊ณผ ์ฃผ๋ฌธ ์ํ๋ ์๋ต์ผ๋ก ์ ์กํ ์ ์๊ฒ ํ๊ธฐ ์ํด ์๋ ์ฝ๋ ์ถ๊ฐ.
1 2 3
private Order.OrderStatus orderStatus; private List<OrderCoffeeResponseDto> orderCoffees; private LocalDateTime createdAt;
-
-
OrderMapper ์์ โ ๋งคํ ์์ ์ด ๋ณต์กํ์ฌ ์ง์ ๋งคํ ๋ฐฉ์์ผ๋ก ๋ณ๊ฒฝ
-
OrderPostDto โ Order๋ก ๋ณ๊ฒฝ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
default Order orderPostDtoToOrder(OrderPostDto orderPostDto) { Order order = new Order(); // (1) order.setMemberId(orderPostDto.getMemberId()); // (2) Set<OrderCoffee> orderCoffees = orderPostDto.getOrderCoffees() .stream() .map(orderCoffeeDto -> // (3) OrderCoffee.builder() .coffeeId(orderCoffeeDto.getCoffeeId()) .quantity(orderCoffeeDto.getQuantity()) .build()) .collect(Collectors.toSet()); order.setOrderCoffees(orderCoffees); return order; }
(1) : OrderPostDto์ ํฌํจ๋ memberId๋ฅผ Order์ ๋ฑ๋ก
(2) : OrderPostDto์ ์ฃผ๋ฌธ๋ ์ปคํผ ์ ๋ณด- List
โ Order ํด๋์ค์ Set
orderCoffees๋ก ๋ณํ -
Set
โ List ๋ก ๋ณ๊ฒฝ ํ๋ ๋ก์ง ํ์ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
default List<OrderCoffeeResponseDto> orderCoffeesToOrderCoffeeResponseDtos (CoffeeService coffeeService,Set<OrderCoffee> orderCoffees){ // (1) return orderCoffees.stream() .map(orderCoffee -> { // (2) Coffee coffee = coffeeService.findCoffee(orderCoffee.getCoffeeId()); return new OrderCoffeeResponseDto(coffee.getCoffeeId(), coffee.getKorName(), coffee.getEngName(), coffee.getPrice(), orderCoffee.getQuantity()); }).collect(Collectors.toList()); } }
โ๏ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ปคํผ์ ๊ตฌ์ฒด์ ์ธ ์ ๋ณด๋ฅผ ์กฐํํ ํ,
OrderCoffeeResponseDto
์ ์ปคํผ ์ ๋ณด๋ฅผ ์ฑ์ ๋ฃ๋ ์ญํ(1) orderCoffees์์ streamํตํด Coffee๋ฅผ ์กฐํํ ํ CoffeeResponseDto๋ก ๋ณํํจ
(2) ํ๋ผ๋ฏธํฐ๋ฅผ ํตํด ๋ค์ด์จ coffeeService๋ก coffeeId ์ ํด๋นํ๋ ์ปคํผ๋ฅผ ์กฐํ
-
Order (with CoffeeService)โ OrderResposeDto ๋ก ๋ณ๊ฒฝ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
default OrderResponseDto orderToOrderResponseDto (CoffeeService coffeeService,Order order){ // (1) long memberId = order.getMemberId(); // (2) List<OrderCoffeeResponseDto> orderCoffees = orderCoffeesToOrderCoffeeResponseDtos (coffeeService, order.getOrderCoffees()); OrderResponseDto orderResponseDto = new OrderResponseDto(); orderResponseDto.setOrderCoffees(orderCoffees); orderResponseDto.setMemberId(memberId); orderResponseDto.setCreatedAt(order.getCreatedAt()); orderResponseDto.setOrderId(order.getOrderId()); orderResponseDto.setOrderStatus(order.getOrderStatus()); return orderResponseDto; }
-
OrderCotroller์ Post์ url ์ถ๊ฐ
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
@RestController
@RequestMapping("/v10/orders")
@Validated
public class OrderController {
private final static String ORDER_DEFAULT_URL = "/v10/orders"; // (1) Default URL ๊ฒฝ๋ก
private final OrderService orderService;
private final OrderMapper mapper;
private final CoffeeService coffeeService;
public OrderController(OrderService orderService,
OrderMapper mapper,
CoffeeService coffeeService) {
this.orderService = orderService;
this.mapper = mapper;
this.coffeeService = coffeeService;
}
@PostMapping
public ResponseEntity postOrder(@Valid @RequestBody OrderPostDto orderPostDto) {
Order order = orderService.createOrder(mapper.orderPostDtoToOrder(orderPostDto));
// (2) ๋ฑ๋ก๋ ์ฃผ๋ฌธ(Resource)์ ํด๋นํ๋ URI ๊ฐ์ฒด
URI location =
UriComponentsBuilder
.newInstance()
.path(ORDER_DEFAULT_URL + "/{order-id}")
.buildAndExpand(order.getOrderId())
.toUri(); // "/v10/orders/{order-id}"
return ResponseEntity.created(location).build(); // (3) HTTP 201 Created status
}
1
<imgs scr="" width=500/>
@Builder
-
@Builder
ํด๋์ค๋ฅผ ์ธ์คํด์คํํ๋ ๋ฐ ํ์ํ ์ฝ๋๋ฅผ ์๋์ผ๋ก ์์ฑ
1 2 3 4 5 6
Person.builder() .name("Adam Savage") .city("San Francisco") .job("Mythbusters") .job("Unchained Reaction") .build();
https://projectlombok.org/features/Builder ์ฐธ๊ณ .
-
์์ฑ์์ Builder
- ์์ฑ์๋ ํ๋ผ๋ฏธํฐ ์์ผ๋ฉด ์๋ฌ. +์์น๋ ๋ง์ถฐ์ผ ํจ
-
๋น๋๋ ํ๋ผ๋ฏธํฐ์ ์์ด๋ ์์์ ์ด๊ธฐํ ํจ. ์์์ ์์น๋ ๋ง์ถค.
โ Setter๋ ์ธ์ ๋ ์ง ์์ ํ ์ ์์ - ๋ณ๊ฒฝ์ ์ฌ์ง๊ฐ ๊ณ์ ์ด๋ฆฌ์ง๋ง Builder๋ ์์ฑ์์ฒ๋ผ ๋ง๋ค์ด์ง ๋ ํ๋ฒ๋ฐ์ ๋ชป ์
- ์ด๊ฑด ํ์ฌ๋ง๋ค ๋ง์ถฐ์ ์ฌ์ฉํ๋ฉด ๋จ โ ์ค๋ฌด๋๊ฐ๋ฉด ๋น๋ ์์ฒญ ๋ง์ด ์ฐ๊ณ ์์ฑ์์ ํผ์ฉํด์ ์
- ํ๋ก๊ทธ๋จ ๋ง๋ค๋ฉด ์ปคํผ์ด๋ฏธ์ง๋ ์ด๋ฐ๊ฑฐ ๋ณด๋ด์ผ ๋๋๋ฐ ์ด๋ด ๋ ๋น๋๊ฐ ํธํจ.
- builder๋ ๊ตฌํํ๊ธฐ ๊น๋ค๋ก์ด๋ฐ ๋กฌ๋ณต์ @Builder ์์ฑํ๋ฉด ํธํ๊ฒ ์ธ ์ ์์ โ ๊ทธ๋๋ ๊ตฌํํ ์ค ์์์ผ ํจ
Leave a comment