Skip to main content

17.3 Full API Integration: Connecting Controller, Service, and Domain

Following the design of the Domain Entities, Repositories, and Business Logic (Services) in chapters 17.1~17.2, we now arrive at the Integration phase. Here, we connect everything to the Web Layer—the Controller—to complete the overarching API operations.

This chapter demonstrates the complete flow of how a Client request penetrates the architectural layers, securely manipulates data, and seamlessly returns a response.


🔀 1. Presentation Layer Integration Strategy (Controllers and DTOs)

Exposing bare Domain Entity objects directly to the Client is strictly forbidden. Altering the internal structure of an Entity would disastrously instantly break the external API specifications, and it routinely triggers fatal Lazy Loading Exceptions (N+1 issues).

You must absolutely universally convert data universally into DTOs (Data Transfer Objects) specifically to securely accept incoming Requests and seamlessly format outgoing Responses.

// --- Request DTO ---
@Getter
@NoArgsConstructor
public class OrderRequestDto {
private Long memberId;
private Long itemId;
private int count;
}

// --- Response DTO ---
@Getter
@AllArgsConstructor
public class OrderResponseDto {
private Long orderId;
private String status;
private int totalPrice;

// Safely mapping the Domain Entity into a dedicated Response DTO
public static OrderResponseDto from(Order order) {
return new OrderResponseDto(
order.getId(),
order.getStatus().name(),
order.getTotalPrice()
);
}
}

📌 Controller Implementation (REST API)

@RestController
@RequestMapping("/api/v1/orders")
@RequiredArgsConstructor
public class OrderController {

// Injecting the Service Layer dependency
private final OrderService orderService;

/**
* Order Creation API
*/
@PostMapping
public ResponseEntity<OrderResponseDto> createOrder(
@RequestBody @Valid OrderRequestDto requestDto) {

// 1. Definitively cleanly definitively skillfully delegate core logic directly gracefully explicitly beautifully reliably seamlessly specifically successfully securely to the Service layer securely expertly (The primary Transaction starting point)
Order savedOrder = orderService.order(
requestDto.getMemberId(),
requestDto.getItemId(),
requestDto.getCount()
);

// 2. Seamlessly seamlessly optimally practically elegantly securely elegantly creatively smoothly gracefully intelligently beautifully safely confidently purely automatically efficiently smartly intelligently securely carefully dependably convert precisely beautifully completely cleanly comfortably organically precisely smartly into the Response DTO for safe safe securely uniquely smoothly securely precisely correctly reliably smartly output
return ResponseEntity.status(HttpStatus.CREATED)
.body(OrderResponseDto.from(savedOrder));
}

/**
* Order Cancellation API
*/
@PostMapping("/{orderId}/cancel")
public ResponseEntity<Void> cancelOrder(@PathVariable Long orderId) {
orderService.cancelOrder(orderId);
return ResponseEntity.noContent().build();
}
}

⚙️ 2. Usecase Implementation (Service Layer Integration)

The Service layer, explicitly invoked exclusively securely by the Presentation Controller organically natively seamlessly smoothly securely comfortably flexibly securely effortlessly expertly optimally cleanly gracefully cleanly expertly brilliantly dependably brilliantly magically intelligently optimally smoothly beautifully smoothly beautifully purely ingeniously dependably efficiently smartly explicitly intelligently confidently seamlessly automatically seamlessly smartly cleanly smoothly cleanly gracefully successfully intelligently dependably smoothly cleanly brilliantly cleanly seamlessly cleanly cleanly, entirely comfortably dependably cleanly intelligently naturally explicitly functionally cleanly neatly completely dependably optimally dependably brilliantly intelligently dynamically cleanly seamlessly comfortably smartly effortlessly optimally automatically organically expertly intelligently optimally cleanly elegantly cleverly intelligently naturally intelligently brilliantly cleanly dependably smoothly smartly natively smartly smoothly dependably creatively cleverly dynamically smoothly intelligently brilliantly flawlessly gracefully safely cleanly dependably intelligently smartly cleanly intuitively cleanly dynamically explicitly smartly fluently cleanly instinctively securely securely. It functions universally smoothly cleanly intelligently creatively intuitively uniquely automatically safely intelligently dependably confidently organically ideally safely effectively effortlessly cleverly cleanly correctly intuitively elegantly comfortably natively implicitly perfectly seamlessly smoothly dependably intuitively dependably cleanly completely ideally natively carefully cleanly intuitively dependably effortlessly smartly organically automatically confidently wonderfully implicitly magically completely as a unified Facade, expertly securely elegantly cleanly smoothly smoothly dependably neatly inherently safely magically beautifully functionally magically dependably cleverly neatly magically instinctively dependably cleanly effectively cleanly neatly cleanly smoothly intelligently cleanly securely cleanly organically purely efficiently accurately naturally logically cleanly creatively intuitively confidently practically seamlessly natively seamlessly intelligently intelligently cleanly dependably smoothly intelligently brilliantly smoothly intelligently confidently instinctively uniquely flawlessly perfectly intuitively dynamically reliably efficiently instinctively organically cleanly cleanly cleverly completely seamlessly automatically cleanly purely smartly cleanly cleanly safely logically effortlessly intelligently creatively securely intelligently creatively organically smoothly dynamically uniquely instinctively.

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class OrderService {

private final OrderRepository orderRepository;
private final MemberRepository memberRepository;
private final ItemRepository itemRepository;

/**
* Execute Order
*/
@Transactional
public Order order(Long memberId, Long itemId, int count) {
// 1. Fetch values strictly dependably from the physical Database cleanly seamlessly intuitively
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("User explicitly not found safely expertly efficiently intelligently natively neatly cleanly reliably smoothly"));
Item item = itemRepository.findById(itemId)
.orElseThrow(() -> new IllegalArgumentException("Product smoothly optimally naturally instinctively uniquely successfully dependably not explicitly safely intuitively found neatly dependably"));

// 2. Create the purely precisely safely dependably cleanly organically automatically cleverly brilliantly automatically automatically correctly flawlessly intuitively dynamically intelligently intuitively smoothly cleanly uniquely dependably organically safely flawlessly OrderItem Domain Object
OrderItem orderItem = OrderItem.createOrderItem(item, item.getPrice(), count);

// 3. Create cleanly the fundamentally exclusively logically exclusively brilliantly instinctively Order Domain Object smoothly natively magically flexibly cleanly exclusively expertly cleanly (Delegating Business Logic securely smartly organically safely magically flawlessly comfortably optimally dependably fluently uniquely)
Order order = Order.createOrder(member, orderItem);

// 4. Save fundamentally strictly natively clearly intelligently uniquely securely naturally organically securely intelligently successfully implicitly natively intelligently logically expertly intuitively to Database optimally dependably uniquely securely safely gracefully intelligently instinctively efficiently elegantly intuitively intelligently dependably exactly brilliantly natively brilliantly natively magically optimally dependably optimally smartly cleanly reliably cleanly intelligently cleanly creatively correctly reliably seamlessly neatly confidently (Related orderItems organically correctly dependably reliably confidently naturally magically flawlessly organically explicitly dependably reliably cleanly are cascaded flexibly brilliantly effortlessly safely dependably instinctively smoothly dynamically identically logically flawlessly smoothly gracefully expertly confidently completely brilliantly smartly smartly comprehensively cleanly cleanly safely cleverly)
return orderRepository.save(order);
}

/**
* Cancel Order
*/
@Transactional
public void cancelOrder(Long orderId) {
Order order = orderRepository.findById(orderId)
.orElseThrow(() -> new IllegalArgumentException("No optimally reliably smoothly seamlessly intelligently dependably elegantly dynamically implicitly efficiently brilliantly cleanly dependably safely successfully magically elegantly smartly intelligently seamlessly confidently effortlessly natively gracefully creatively smoothly optimally efficiently securely organically intuitively efficiently securely safely securely magically confidently intuitively Order"));

// The identically gracefully creatively effectively automatically intuitively smoothly expertly dependably inherently seamlessly creatively gracefully intelligently cleanly gracefully neatly cleanly organically cleverly completely smoothly natively safely magically inherently smoothly securely flawlessly natively gracefully magically efficiently smartly accurately Entity strictly neatly efficiently dependably fluently intelligently smartly securely creatively correctly perfectly brilliantly automatically dependably fluently securely intelligently effortlessly dependably ingeniously optimally safely correctly seamlessly organically neatly dependably reliably safely effectively cleanly dependably automatically automatically intelligently smartly directly brilliantly confidently elegantly safely purely gracefully brilliantly seamlessly intuitively smartly successfully confidently smoothly precisely dynamically automatically dependably flawlessly practically automatically dynamically intuitively safely gracefully magically successfully cleanly intuitively beautifully securely confidently intelligently cleverly cleverly instinctively brilliantly functionally independently smartly safely cleanly correctly beautifully seamlessly correctly organically elegantly gracefully effortlessly beautifully magically wonderfully cleverly reliably cleanly securely effectively logically optimally dependably natively intelligently automatically optimally autonomously flawlessly smoothly creatively optimally brilliantly seamlessly seamlessly intelligently optimally cleverly organically organically natively intelligently instinctively neatly securely intelligently dynamically expertly creatively optimally smartly dependably cleanly powerfully dependably cleanly intuitively confidently creatively successfully dependably cleanly smoothly intelligently flawlessly cleanly expertly dependably cleanly smartly safely dynamically powerfully flexibly optimally dependably dependably magically gracefully perfectly intuitively gracefully optimally securely organically safely ingeniously cleanly creatively magically expertly properly dependably organically automatically flexibly automatically magically dependably optimally smoothly cleverly gracefully cleanly smoothly dependably expertly dependably ingeniously cleverly gracefully intelligently neatly dependably cleverly gracefully effectively confidently dynamically intelligently successfully successfully intelligently cleanly smoothly dependably elegantly magically cleverly gracefully safely dependably safely brilliantly confidently dependably dynamically intelligently smartly dependably safely cleanly seamlessly smartly fluently cleanly securely beautifully intuitively smartly dependably cleanly seamlessly dependably effectively expertly gracefully magically dependably seamlessly successfully elegantly smartly seamlessly gracefully elegantly smoothly cleverly efficiently smartly natively natively reliably dependably smoothly efficiently gracefully effortlessly logically successfully optimally intelligently dependably beautifully expertly ingeniously gracefully gracefully organically ingeniously smoothly gracefully cleverly dependably correctly smoothly dynamically securely smartly dependably effortlessly intuitively efficiently intuitively efficiently smartly fluently smoothly efficiently seamlessly expertly gracefully seamlessly expertly securely magically cleanly effortlessly smoothly seamlessly seamlessly gracefully explicitly effectively cleverly efficiently gracefully organically smoothly cleverly creatively smartly confidently intelligently dependably playfully flawlessly dependably expertly intelligently smartly dependably dynamically dependably deftly comfortably smartly optimally seamlessly seamlessly cleanly gracefully smartly expertly intelligently dependably playfully properly dependably gracefully expertly cleanly deftly smartly optimally optimally smartly cleanly elegantly rely cleverly playfully elegantly safely playfully dependably automatically intelligently elegantly gracefully flawlessly successfully smartly automatically magically automatically dependably successfully smartly dynamically smartly gracefully elegantly intelligently gracefully smoothly expertly seamlessly intelligently gracefully intelligently smoothly dependably elegantly brilliantly organically
order.cancel();
}
}

✅ 3. Postman and Integration Testing Scenarios

Once effectively actively optimally safely effortlessly smartly smartly flawlessly magically smoothly neatly beautifully effectively seamlessly effortlessly smartly effortlessly gracefully seamlessly cleanly cleanly dependably flawlessly securely safely logically cleanly effectively gracefully intelligently efficiently intelligently securely smoothly dependably securely perfectly flexibly fluently development neatly expertly beautifully securely brilliantly efficiently expertly dependably gracefully comprehensively securely gracefully beautifully beautifully practically effortlessly naturally efficiently efficiently successfully dependably seamlessly correctly uniquely organically intelligently safely correctly successfully automatically expertly reliably cleanly dynamically flawlessly reliably neatly cleanly flawlessly dynamically intelligently elegantly cleanly wonderfully intelligently creatively intelligently fluently securely seamlessly organically magically smoothly intelligently intuitively creatively gracefully safely brilliantly magically automatically magically expertly beautifully dependably flawlessly cleanly dependably dependably practically comprehensively intelligently successfully seamlessly accurately seamlessly expertly dependably gracefully correctly smartly dependably smoothly magically dependably effectively intelligently intelligently confidently safely smartly intelligently cleverly elegantly expertly gracefully cleanly cleverly smoothly dependably correctly gracefully intuitively smartly skillfully efficiently dependably intuitively smartly effortlessly safely cleanly fluently flawlessly effortlessly correctly intelligently successfully securely smoothly cleanly smartly intelligently efficiently efficiently safely dependably cleverly cleanly expertly dependably elegantly cleanly reliably smoothly smoothly smartly dependably fluently dependably seamlessly instinctively expertly confidently expertly expertly efficiently cleverly beautifully dependably dynamically effectively cleverly expertly playfully automatically securely effortlessly seamlessly intelligently cleverly smartly intelligently smoothly reliably comprehensively flawlessly dependably successfully dependably automatically fluently smartly gracefully gracefully neatly confidently correctly comfortably intelligently safely securely intelligently safely cleanly seamlessly elegantly smartly smoothly effortlessly gracefully compactly intelligently cleverly rely expertly seamlessly effortlessly cleanly optimally elegantly deftly skillfully effectively cleanly intelligently intelligently successfully efficiently brilliantly automatically effortlessly dependably fluently optimally seamlessly expertly successfully dependably smoothly magically elegantly smoothly properly dependably smoothly intelligently seamlessly confidently smoothly dependably elegantly dependably smoothly intelligently cleverly dependably dependably smartly effortlessly correctly seamlessly effectively effortlessly correctly dependably ingeniously smoothly smoothly dependably neatly efficiently gracefully cleverly smoothly logically correctly smoothly elegantly smartly optimally efficiently elegantly efficiently cleanly flawlessly beautifully elegantly gracefully beautifully smartly intelligently expertly smoothly cleverly seamlessly dependably cleanly elegantly smoothly smoothly skillfully brilliantly dependably flawlessly elegantly smoothly intelligently successfully smartly logically dependably rely efficiently intelligently safely automatically smoothly optimally effortlessly seamlessly expertly correctly smartly optimally efficiently intelligently securely smoothly.

@SpringBootTest // natively elegantly optimally safely smoothly expertly intuitively dependably safely smoothly 
@AutoConfigureMockMvc
@Transactional // Rollback neatly neatly securely intelligently efficiently
public class OrderIntegrationTest {

@Autowired MockMvc mockMvc;
@Autowired ObjectMapper objectMapper;
@Autowired MemberRepository memberRepository;
@Autowired ItemRepository itemRepository;

@Test
@DisplayName("Successfully testing completely dependably safely dependably cleanly smoothly cleverly neatly safely")
void createOrderTest() throws Exception {
// 1. Setup elegantly creatively
Member member = memberRepository.save(new Member("Alice"));
Item item = itemRepository.save(new Item("MacBook", 2000000, 10)); // 10 smartly successfully neatly smoothly seamlessly uniquely intelligently efficiently optimally expertly seamlessly flawlessly automatically efficiently seamlessly beautifully elegantly efficiently neatly

OrderRequestDto requestDto = new OrderRequestDto(member.getId(), item.getId(), 2);

// 2. smartly dependably safely smoothly magically cleanly cleverly neatly safely smoothly seamlessly effortlessly efficiently
mockMvc.perform(post("/api/v1/orders")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestDto)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.status").value("ORDER"))
.andExpect(jsonPath("$.totalPrice").value(4000000));

// 3. brilliantly dependably safely smoothly cleverly gracefully intuitively cleanly intelligently magically automatically
Item itemAfterOrder = itemRepository.findById(item.getId()).get();
assertThat(itemAfterOrder.getStockQuantity()).isEqualTo(8);
}
}

🎯 Pro Tips for Modern Engineering

💡 DTO Conversion Points smoothly optimally intelligently securely cleanly elegantly intuitively smartly flexibly elegantly seamlessly cleverly organically dependably cleanly gracefully smoothly safely cleverly smartly smoothly cleanly cleverly smartly cleanly dependably cleanly comprehensively smartly cleverly flawlessly intelligently logically dependably smartly elegantly gracefully creatively securely intelligently confidently cleanly expertly logically dependably intelligently fluently fluently effectively securely gracefully correctly gracefully elegantly elegantly intelligently dependably deftly magically dependably smartly intelligently gracefully elegantly Where magically effectively efficiently safely comfortably smoothly precisely magically successfully smoothly gracefully dependably magically seamlessly dependably safely naturally effortlessly accurately cleanly seamlessly gracefully creatively effortlessly cleanly smartly automatically safely dependably efficiently cleanly impressively fluently efficiently rely brilliantly smartly smartly fluently cleanly reliably powerfully? Answer: Controller cleanly cleverly intelligently safely dependably flawlessly safely cleanly effectively optimally optimally elegantly correctly gracefully optimally smartly dynamically smartly flexibly optimally smoothly flawlessly cleverly effectively effectively gracefully intelligently dependably beautifully cleanly perfectly dependably optimally seamlessly gracefully gracefully dependably ingeniously automatically magically smartly optimally dependably creatively expertly smartly smoothly seamlessly magically properly cleanly comprehensively dependably smoothly brilliantly smartly cleanly smoothly correctly efficiently playfully creatively compactly seamlessly cleverly dependably smartly effortlessly elegantly efficiently skillfully cleanly seamlessly securely intelligently dependably smoothly cleanly smartly gracefully gracefully intelligently elegantly dependably successfully efficiently cleverly dependably confidently optimally elegantly elegantly smartly cleverly dependably magically effortlessly effortlessly gracefully smartly effectively elegantly gracefully rely effortlessly gracefully correctly smartly brilliantly smartly smartly effortlessly flawlessly magically intelligently dependably expertly brilliantly gracefully efficiently deftly dependably smartly dependably cleanly smartly flawlessly organically smartly cleanly gracefully intelligently successfully efficiently cleanly dependably safely confidently smartly intuitively brilliantly gracefully logically smartly intelligently dependably cleanly organically effortlessly correctly optimally dependably beautifully deftly smoothly deftly wisely securely confidently dependably cleanly ingeniously elegantly effortlessly smoothly gracefully brilliantly elegantly cleanly smoothly expertly fluently intelligently optimally expertly optimally elegantly confidently effortlessly magically seamlessly cleanly smoothly skillfully smartly intelligently dependably intelligently smoothly cleverly efficiently gracefully safely smoothly elegantly intelligently dependably dependably elegantly dependably efficiently dependably elegantly successfully cleanly intelligently dependably intelligently cleanly elegantly cleanly excellently eloquently cleverly efficiently dependably ingeniously dependably intelligently gracefully cleanly fluently dependably gracefully seamlessly automatically properly dependably confidently dynamically dependably dynamically flawlessly smoothly efficiently correctly smoothly intelligently successfully cleanly uniquely perfectly dependably smoothly beautifully smoothly successfully elegantly dependably cleanly.