Architecting Validation Logic: A Java/Spring Boot Implementation in Clean Architecture

<h2>In the process of developing and implementing a new service/API, sometimes the question arises regarding where to place the validation logic. I will attempt to provide a perspective on its placement and provide an example of its implementation in Clean Architecture using Java/Spring Boot, utilizing Exceptions (`jakarta`) and Non-Exceptions (`vavr`).</h2> <p><a href="https://semotpan.com/?source=post_page-----b1236dd82ef6--------------------------------" rel="noopener ugc nofollow"><img alt="Serghei Motpan" src="https://miro.medium.com/v2/resize:fill:44:44/1*[email protected]" style="height:44px; width:44px" /></a></p> <p><a href="https://semotpan.com/?source=post_page-----b1236dd82ef6--------------------------------" rel="noopener ugc nofollow">Serghei Motpan</a></p> <p>&middot;</p> <p><a href="https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fsubscribe%2Fuser%2F5025d7071baa&amp;operation=register&amp;redirect=https%3A%2F%2Fsemotpan.com%2Farchitecting-validation-logic-a-java-spring-boot-implementation-in-clean-architecture-b1236dd82ef6&amp;user=Serghei+Motpan&amp;userId=5025d7071baa&amp;source=post_page-5025d7071baa----b1236dd82ef6---------------------post_header-----------" rel="noopener follow">Follow</a></p> <p>15 min read</p> <p>&middot;</p> <p>Jul 10</p> <p>&nbsp;</p> <p>106</p> <p>&nbsp;</p> <p>4</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p><img alt="" src="https://miro.medium.com/v2/resize:fit:630/1*VFEODNNa-MgNGZeSStVHhw.png" style="height:190px; width:700px" /></p> <h1><strong>Table of content</strong></h1> <ul> <li>Validation in context</li> <li>Test Driven Development (Contract-First approach)</li> <li>Organising Code</li> <li>Implementing a Use Case in Spring Boot</li> <li>Conclusions</li> </ul> <p>This article covers various topics, but it will not delve extensively into subjects such as architecture, spring-boot exception handling, optimal library usage, TDD, or Contract First Testing. It assumes that you already have some knowledge about these topics or can refer to the resources provided at the end of the article for further information. The main focus of the article will be on validations, specifically addressing them within the context of Clean Architecture and Hexagonal Architecture.</p> <h1>Validation in context</h1> <p>The concept of &ldquo;validation&rdquo; is broad and ambiguous as it carries different interpretations depending on the context in which it is used. However, there is a common misconception that validation only serves a singular purpose. In reality, validation plays a role in various aspects. Similar to&nbsp;<em>Clean Architecture</em>, where responsibilities are divided into layers, each layer possesses its own validation logic.</p> <p><a href="https://semotpan.com/architecting-validation-logic-a-java-spring-boot-implementation-in-clean-architecture-b1236dd82ef6">Click Here</a></p>