<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Anshul Bansal</title>
    <description>The latest articles on Forem by Anshul Bansal (@smartyansh).</description>
    <link>https://forem.com/smartyansh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F237015%2F346d4930-2a19-49bf-8244-0e73f6d6f0fe.jpeg</url>
      <title>Forem: Anshul Bansal</title>
      <link>https://forem.com/smartyansh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/smartyansh"/>
    <language>en</language>
    <item>
      <title>10 features of a perfect BRD (business requirement document)</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Mon, 15 May 2023 12:25:18 +0000</pubDate>
      <link>https://forem.com/smartyansh/10-features-of-a-perfect-business-requirement-document-3312</link>
      <guid>https://forem.com/smartyansh/10-features-of-a-perfect-business-requirement-document-3312</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In this post, we'll explain a business requirement document (BRD) and the 10 most important features of a perfect one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a business requirement document (BRD)?
&lt;/h2&gt;

&lt;p&gt;A document that discusses the business requirement of a project is called BRD. A business requirement document (BRD) is a crucial tool that outlines the objectives, scope, and specific requirements of a project. &lt;br&gt;
BRD helps to bring everyone to the table to understand the business requirements in detail and the outcome of it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Important points to include:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Project Overview
&lt;/h3&gt;

&lt;p&gt;The project overview section provides a high-level summary of the project, including its purpose, goals, and the problem it aims to solve. It sets the context for the document, ensuring that everyone understands the project's background and objectives. In short -  Whys and Whats are usually specs out here.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Stakeholders
&lt;/h3&gt;

&lt;p&gt;Stakeholders cover all the persons involved in the business and feature discussions of the project - business owners, project managers, developers, designers, and any other key individuals who will contribute to or be affected by the project. Clearly defining stakeholders ensures effective communication and collaboration throughout the project lifecycle.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Objective
&lt;/h3&gt;

&lt;p&gt;State the clear and specific objective of the project. This section articulates the desired outcome or deliverable that the project intends to achieve. It serves as a guiding principle for decision-making and helps align the team toward a common goal.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Success Metrics
&lt;/h3&gt;

&lt;p&gt;Define the metrics or criteria that will determine the success of the project. These can be quantitative or qualitative indicators that measure the project's performance against the objectives. Clearly defined success metrics enable effective evaluation and assessment of the project's outcomes.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Assumptions
&lt;/h3&gt;

&lt;p&gt;A section to list down any assumptions to consider while working on the project. Assumptions help to set the context and manage expectations. By identifying and documenting assumptions, the project team can address potential risks and uncertainties upfront.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Business requirements
&lt;/h3&gt;

&lt;p&gt;This section mentions all business requirements of this project. Each requirement should be actionable and measurable to facilitate effective implementation. Hence, these business requirements will translate into features that will further convert into epics/stories/tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Open questions
&lt;/h3&gt;

&lt;p&gt;While discussing the business requirement, if stakeholders come across questions that aren't answered or need further clarification. This section captures those open questions and serves as a reference for ongoing discussions and issue resolution.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Scope
&lt;/h3&gt;

&lt;p&gt;A clear scope of the project sets the boundaries for the teams. This includes what is included in the project as well as what is excluded. Defining the scope helps manage expectations and ensures that the project remains focused and achievable within the given constraints.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Quality control
&lt;/h3&gt;

&lt;p&gt;Describe the processes and procedures that will be employed to ensure the quality of the project deliverables. This includes answers to questions like How will this project be tested? How will the team ensure the business requirements are met?&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Business constraints
&lt;/h3&gt;

&lt;p&gt;Identify and document any constraints or limitations of the project, such as budget, deadlines, resources, etc. Understanding and considering these constraints helps manage expectations and ensures realistic planning and execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this quick post, we've discussed the 10 key features that make a perfect business requirement document (BRD) that includes a comprehensive project overview, clearly defined objectives, success metrics, and stakeholder data, and addressing important aspects such as assumptions, open questions, scope, quality control, and business constraints. &lt;/p&gt;

&lt;p&gt;A well-crafted BRD becomes a valuable guide for successful project execution. By investing time and effort into it, we set the stage for effective communication, collaboration, and project success.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Training Curriculum for Junior Programmers</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Thu, 27 May 2021 13:42:45 +0000</pubDate>
      <link>https://forem.com/smartyansh/training-curriculum-for-junior-programmers-3do3</link>
      <guid>https://forem.com/smartyansh/training-curriculum-for-junior-programmers-3do3</guid>
      <description>&lt;h1&gt;
  
  
  1. Overview
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Many of us are self-taught programmers in software development. Therefore, we often require a curriculum to learn and master the art of programming.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Not only a curriculum helps to achieve the learning goal efficiently but, it promotes self-discipline too.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this article, we'll discuss a Training Curriculum for Full Stack Java Programmers (Junior).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Such a curriculum is usually used in companies like &lt;a href="https://www.tcs.com/"&gt;TCS&lt;/a&gt; and &lt;a href="https://www.infosys.com/"&gt;Infosys&lt;/a&gt; to groom fresh talents joining as Trainee Engineers (or Freshers). It helps them to learn software development, improves efficiency, and makes them ready for future assignments.&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/smartyansh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3ISkhezr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--dvR1Jwld--/c_imagga_scale%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/237015/346d4930-2a19-49bf-8244-0e73f6d6f0fe.jpeg" alt="smartyansh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/smartyansh/what-all-i-ve-tried-in-my-8-years-of-programming-544c" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;My Programming Journey to Senior Programmer&lt;/h2&gt;
      &lt;h3&gt;Anshul Bansal ・ Oct 18 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#codenewbie&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#productivity&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;h1&gt;
  
  
  2. Training Curriculum
&lt;/h1&gt;

&lt;p&gt;This training curriculum is in two phases - Foundation and Advanced Phase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Foundation Phase focuses on concept building through theory and quick hands-on.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Furthermore, &lt;strong&gt;the Advanced Phase lets the programmers create an end-to-end web application utilizing all the learnings of the Foundation Phase&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Foundation Phase
&lt;/h1&gt;

&lt;p&gt;The foundation phase is about learning the &lt;a href="https://www.tutorialspoint.com/java/java_quick_guide.htm"&gt;Java programming language&lt;/a&gt; along with &lt;a href="https://www.javatpoint.com/java-oops-concepts"&gt;OOP concepts&lt;/a&gt; and advanced features like &lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/collections-in-java-2/"&gt;Collections&lt;/a&gt;, &lt;a href="https://www.javatpoint.com/multithreading-in-java"&gt;Multi-Threading&lt;/a&gt;, &lt;a href="https://www.javatpoint.com/Garbage-Collection"&gt;Garbage Collection&lt;/a&gt;. Then, we should focus on learning &lt;a href="https://www.techonthenet.com/postgresql/index.php"&gt;Database&lt;/a&gt;, &lt;a href="https://www.techonthenet.com/sql/index.php"&gt;SQL&lt;/a&gt;, and &lt;a href="https://www.w3schools.com/html/"&gt;WEB UI&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Afterward, the knowledge of a few build tools like &lt;a href="https://www.baeldung.com/gradle"&gt;Gradle&lt;/a&gt;, frameworks like &lt;a href="https://www.baeldung.com/junit-5"&gt;JUnit&lt;/a&gt;, helper tools like &lt;a href="https://www.atlassian.com/git/tutorials"&gt;GIT&lt;/a&gt; &amp;amp; and &lt;a href="https://www.tutorialspoint.com/sdlc/sdlc_overview.htm"&gt;SDLC&lt;/a&gt; methodologies like &lt;a href="https://www.atlassian.com/agile"&gt;Agile&lt;/a&gt; will also be required.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J-zvUVf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bs3pbrlhwg0fwu0bzxmg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J-zvUVf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bs3pbrlhwg0fwu0bzxmg.png" alt="Foundation Phase" width="800" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, I've put each step into a box to segregate them from others. Also, we don't have a particular order of the steps. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;A trainee can self-decide his order, moving from more interesting/relevant topics to the less significant ones&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Also, don't forget to follow a code review checklist and best practices for delivering better code.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/smartyansh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3ISkhezr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--dvR1Jwld--/c_imagga_scale%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/237015/346d4930-2a19-49bf-8244-0e73f6d6f0fe.jpeg" alt="smartyansh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/smartyansh/code-review-checklist-for-java-beginners-181f" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Code Review Checklist for Java Beginners&lt;/h2&gt;
      &lt;h3&gt;Anshul Bansal ・ Jan 5 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#java&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#productivity&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#codequality&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;div class="ltag__link"&gt;
  &lt;a href="/smartyansh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3ISkhezr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--dvR1Jwld--/c_imagga_scale%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/237015/346d4930-2a19-49bf-8244-0e73f6d6f0fe.jpeg" alt="smartyansh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/smartyansh/coding-best-practices-for-java-web-apps-59m2" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Coding Best Practices for Java Apps&lt;/h2&gt;
      &lt;h3&gt;Anshul Bansal ・ Nov 16 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#java&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#codequality&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#productivity&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;h1&gt;
  
  
  4. Advanced Phase
&lt;/h1&gt;

&lt;p&gt;The Advanced Phase is the time to use the skills developed in the Foundation Phase. &lt;/p&gt;

&lt;p&gt;It's all about integrating the learning to create a Web Application, follow a few steps of the SDLC like code review and code check-in, and learn about frameworks like &lt;a href="https://spring.io/projects/spring-framework"&gt;Spring&lt;/a&gt;, &lt;a href="https://hibernate.org/"&gt;Hibernate&lt;/a&gt;, and &lt;a href="https://grails.org/"&gt;Grails&lt;/a&gt; that add value in day-to-day programming activities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b90XZUzK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnpcp404jwgxd38urc36.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b90XZUzK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnpcp404jwgxd38urc36.png" alt="Advanced Phase" width="800" height="603"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  5. Extras
&lt;/h1&gt;

&lt;p&gt;Here, let's discuss a few other things that can give an extra edge to a Programmer in their day-to-day work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data Structures&lt;/li&gt;
&lt;li&gt;Algorithms&lt;/li&gt;
&lt;li&gt;Design Patterns&lt;/li&gt;
&lt;li&gt;Data Formats - JSON/XML&lt;/li&gt;
&lt;li&gt;Functional Programming - Lambda Expressions, Java Streams&lt;/li&gt;
&lt;li&gt;Scripting - Groovy/Python&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  6. Conclusion
&lt;/h1&gt;

&lt;p&gt;In this article, we discussed a training curriculum for Junior Programmers that aspire to be Java Full Stack Programmer and want to be productive quickly.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;First, we explored the Foundation Phase that helps to develop a few skills. Then, we examined the Advanced Phase that prepares a programmer to utilize each skill learned in the Foundation Phase to be productive.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Last, we discussed some bonus concepts that can give an extra edge to a programmer over others.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Although, the curriculum is mainly targeting Java developers. However, we can replace Java with any technology like .Net, Python, and C++. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The idea is to learn the basics of a programming language with tools and methodologies that help a Full Stack Programmer be efficient, productive, and competent.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Please let me know your thoughts on the points discussed.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>training</category>
      <category>java</category>
    </item>
    <item>
      <title>Properties Of A Database Transaction</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Tue, 11 May 2021 15:53:08 +0000</pubDate>
      <link>https://forem.com/smartyansh/properties-of-a-database-transaction-1pae</link>
      <guid>https://forem.com/smartyansh/properties-of-a-database-transaction-1pae</guid>
      <description>&lt;h1&gt;
  
  
  1. Overview
&lt;/h1&gt;

&lt;p&gt;In this short article, we'll revisit the properties of the Database Transaction.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. What Is A Transaction?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;A transaction is the unit of work performed on the database&lt;/strong&gt;.&lt;br&gt;
For example, &lt;em&gt;insert/update a record in a table&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It could also contain multiple separate actions performed together but sequentially. For instance, &lt;em&gt;insert a row in one table, update a row in the second table, and delete a row from another table&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A more practical example can be - &lt;em&gt;adding a credit record in the Payment table and then updating the balance in the Accounts table&lt;/em&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Properties Of A Transaction
&lt;/h1&gt;

&lt;p&gt;Every database transaction needs to mandatorily have a few properties to provide data reliability and integrity. These properties are called &lt;strong&gt;ACID&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;acronym stands for Atomicity, Consistency, Isolation, and Durability&lt;/strong&gt;. Each property is inter-dependent and together helps to achieve data reliability and integrity.&lt;/p&gt;

&lt;p&gt;Let's discuss them in detail to better our understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.1. Atomicity
&lt;/h2&gt;

&lt;p&gt;Transactions often perform multiple separate actions. &lt;br&gt;
So, a transaction that performed a few changes and missed others can result in the wrong data. Therefore, &lt;strong&gt;a transaction must perform every action or nothing at all&lt;/strong&gt;. Simply put, &lt;strong&gt;Atomicity means all or nothing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For instance, a transaction inserted a credit record in the Payment table and couldn't update the balance in the Accounts table (due to a failure or mishap). Then, the accounting system will result in data error, and customers could see wrong entries/calculations in their accounts&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, &lt;strong&gt;a transaction must ensure either all the operations are performed or rolled back all the changes&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.2. Consistency
&lt;/h2&gt;

&lt;p&gt;A transaction must always have consistent data before the start and after it ends to ensure data integrity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If a transaction violates the database consistency rules, then the entire transaction should rollback&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, &lt;em&gt;in a banking system, when transferring funds from one account to another, if a transaction manages to credit funds in one account and misses to debit funds from another will result in funds mismanagement. Hence, the total funds of the bank will go wrong&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.3. Isolation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Each transaction should be isolated and performed in the total separation&lt;/strong&gt;. So, the transactions that happen at the same time don't see each other changes until completed.&lt;/p&gt;

&lt;p&gt;For example, &lt;em&gt;suppose User A sends money to User B, and at the same time, User B sends money to User C. &lt;br&gt;
Now, the transaction by User A and User B are concurrent and tries to update the balances at the same time. Therefore, both of the transactions must be isolated, not affecting each other changes and resulting in data error&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;A database usually uses locking principles to achieve isolation of the transactions. In most cases, whichever transaction reaches first will get the lock of the record, performs the changes in separation. Then, another transaction requesting the lock performs the changes on the already updated data. &lt;/p&gt;

&lt;p&gt;There are two most common database locking strategies - &lt;strong&gt;Optimistic and Pessimistic Locking to achieve the isolation of the transactions&lt;/strong&gt;. We'll try to cover these in detail in another write-up.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.4. Durability
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A transaction must be durable to never undone the persisted changes even in uncertain events like power failure and abnormal termination&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Similar to our already discussed example, &lt;em&gt;an application that transfers funds from one account to another, the durability property ensures the changes on the Accounts table (through a single transaction) won't be reversed in case of any mishap&lt;/em&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Conclusion
&lt;/h1&gt;

&lt;p&gt;In this brief article, we briefly discussed the database transaction and its &lt;strong&gt;ACID&lt;/strong&gt; properties.&lt;/p&gt;

&lt;p&gt;Please let me know your thoughts in the comments.&lt;br&gt;
Thanks for reading!&lt;/p&gt;

</description>
      <category>database</category>
      <category>transaction</category>
      <category>acid</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Good API Design and Why It Matters?</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Sun, 27 Dec 2020 21:45:33 +0000</pubDate>
      <link>https://forem.com/smartyansh/good-api-design-and-why-it-matters-2df5</link>
      <guid>https://forem.com/smartyansh/good-api-design-and-why-it-matters-2df5</guid>
      <description>&lt;h2&gt;
  
  
  1. Overview
&lt;/h2&gt;

&lt;p&gt;In this article, we'll discuss How to design a Good API and Why it matters to each of us (I'm talking to the Programmers/Developers)?&lt;/p&gt;

&lt;p&gt;Note: This article is an excerpt of Joshua Bloch's talk -  &lt;a href="http://www.youtube.com/watch?v=heh4OeB9A-c" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;How to Design a Good API and Why it Matters?&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know many of us would've already seen that video. So, we'll be going through Josh's recommendations for a Good API design and touch our basics again. By chance, if you are new, feel free to explore his genius.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What is an API?
&lt;/h2&gt;

&lt;p&gt;An API (Application Programming Interface) is &lt;strong&gt;a contract between the user and the software&lt;/strong&gt;. &lt;br&gt;
It's &lt;strong&gt;an interface for the user to interact with the software, perform actions, and exchange data in the prescribed formats&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It can be as simple as a few methods available in a class to as complex as the web services for the enterprise software.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Why it Matters to Us?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Every programmer is an API designer

&lt;ul&gt;
&lt;li&gt;Whether designing a class that performs some actions or implementing web services&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Public APIs are forever, so we get only one chance to get it right

&lt;ul&gt;
&lt;li&gt;Once APIs are published in public, it's impossible to take them back&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A good API wins customers

&lt;ul&gt;
&lt;li&gt;Assets for the corporates and lasts longer&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Improves code quality, reusability, and maintainability of the software

&lt;ul&gt;
&lt;li&gt;Good design usually coincides with good performance&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  4. What is a Good API?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Easy to learn&lt;/li&gt;
&lt;li&gt;Easy to use, even without documentation&lt;/li&gt;
&lt;li&gt;Hard to misuse&lt;/li&gt;
&lt;li&gt;Easy to read and maintain code that uses it&lt;/li&gt;
&lt;li&gt;Sufficiently powerful to satisfy requirements&lt;/li&gt;
&lt;li&gt;Easy to extend and evolve&lt;/li&gt;
&lt;li&gt;Appropriate to its audience&lt;/li&gt;
&lt;li&gt;Coexists peacefully with the platform&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  5. Tips for Creating a Good API
&lt;/h2&gt;
&lt;h3&gt;
  
  
  5.1. Get the Specs Right
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Start with a Short Spec - 1 Page should be Ideal&lt;/li&gt;
&lt;li&gt;Extract true requirements out of proposed solutions&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  5.2. API Should Do One Thing and Do it Well
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Design and implement API keeping one problem in mind.&lt;/strong&gt;&lt;br&gt;
We shouldn't try to solve a lot of problems in a single API. Better, go with a set of APIs to address different needs.&lt;/p&gt;

&lt;p&gt;For instance, the &lt;em&gt;LocalDateTime&lt;/em&gt; and &lt;em&gt;CompletableFuture&lt;/em&gt; classes in Java solve the problem of Date/Time and Asynchronous programming very well.&lt;/p&gt;
&lt;h3&gt;
  
  
  5.3. API Should be as Small as Possible but No Smaller
&lt;/h3&gt;

&lt;p&gt;Ideally, APIs should be small, simple, and clean. However, sometimes, &lt;strong&gt;it's easier and more rewarding to build something more general&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  5.4. Don't Violate the Principle of Least Astonishment
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Don't surprise the user by the behavior of APIs.&lt;/strong&gt;&lt;br&gt;
For instance, the method name and its action should be aligned mutually.&lt;/p&gt;
&lt;h3&gt;
  
  
  5.5. When in Doubt, Leave it Out
&lt;/h3&gt;

&lt;p&gt;It's always easier to add to the API (in further iterations). But, &lt;strong&gt;we can't remove anything from it once published&lt;/strong&gt; - as APIs should honor the backward compatibility to avoid any hassle for the existing implementations of the consumer.&lt;/p&gt;
&lt;h3&gt;
  
  
  5.6. Never Leak the Implementation Details
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;When implementation details get leaked through documents, it's harder to change or modify the implementations later.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For instance, a user may rely on the implementation details and use them in the features. So, this could lead to disaster on the consumer side if the implementation modifies in the APIs.&lt;/p&gt;
&lt;h2&gt;
  
  
  6. Implementation Guidelines for a Good API
&lt;/h2&gt;
&lt;h3&gt;
  
  
  6.1. Naming Matter
&lt;/h3&gt;

&lt;p&gt;Follow naming conventions diligently. Also, names and their behaviors should be consistent throughout. &lt;br&gt;
For instance, the same name shouldn't mean different in the various parts of APIs. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;APIs should read like prose and never astonish the consumer by their behaviors that go against their name.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  6.2. Minimize Accessibility of Everything
&lt;/h3&gt;

&lt;p&gt;For instance, use Private modifiers for the variables thoroughly.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.3. Minimize Mutability
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Without a good reason, we shouldn't keep our classes mutable.&lt;/strong&gt;&lt;br&gt;
Immutable classes are simple, concise, and doesn't need synchronization handling in the concurrent transactions.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.4. Fail Fast
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Report errors as soon as possible after they occur.&lt;/strong&gt; &lt;br&gt;
Usually, compile-time is the best, and that's why we should take advantage of generics/static typings.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.5. Use Appropriate Parameter and Return Types
&lt;/h3&gt;

&lt;p&gt;Consider specific parameter types over the String. The same goes with BigDecimal over float/double for the monetary values. &lt;/p&gt;

&lt;p&gt;Also, try to use the same parameter/return types for similar data exchange.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.6. Avoid Long Parameter Lists
&lt;/h3&gt;

&lt;p&gt;A long list of parameter lists is often confusing and error-prone when similar typed. &lt;br&gt;
Ideally, it shouldn't be more than 3-4.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.7. Use Consistent Parameter Ordering Across Methods
&lt;/h3&gt;

&lt;p&gt;For ease of use, we should keep the order of the parameters similar in various actions.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.8. Avoid Return Types that Demand Exceptional Processing
&lt;/h3&gt;

&lt;p&gt;For example, when dealing with the collection return type, the consumer shouldn't bother about handling a null value, which is prone to the &lt;em&gt;NullPointerException&lt;/em&gt;. Rather we should return an empty collection.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.9. Documentation Matters
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Documentation is a must for a good API design, its popularity, and success.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can see, all Java APIs and popular libraries are very well documented.&lt;br&gt;
Therefore, we should document everything like class, interface, method, constructor, parameter, and exception. Also, we must state the preconditions, postconditions, and side-effects for any action/behavior.&lt;/p&gt;

&lt;p&gt;At the same time, as we've discussed already, we should never document implementation details.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.10. Test with Two-Three Plugins Before Release
&lt;/h3&gt;

&lt;p&gt;We should test our APIs very-well before the release. For that, &lt;strong&gt;write two to three plugins for the APIs to make sure it will mostly behave as expected&lt;/strong&gt; for future consumption.&lt;/p&gt;
&lt;h2&gt;
  
  
  7. Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we've seen a few recommendations for a Good API design, some tips and tricks for the implementation, and why it matters to us?&lt;/p&gt;

&lt;p&gt;For Coding Best Practices take a look:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/smartyansh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F237015%2F346d4930-2a19-49bf-8244-0e73f6d6f0fe.jpeg" alt="smartyansh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/smartyansh/coding-best-practices-for-java-web-apps-59m2" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Coding Best Practices for Java Apps&lt;/h2&gt;
      &lt;h3&gt;Anshul Bansal ・ Nov 16 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#java&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#codequality&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#productivity&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;Please share your thoughts in the comments.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>design</category>
      <category>architecture</category>
      <category>oop</category>
    </item>
    <item>
      <title>Coding Best Practices for Java Apps</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Mon, 16 Nov 2020 12:11:41 +0000</pubDate>
      <link>https://forem.com/smartyansh/coding-best-practices-for-java-web-apps-59m2</link>
      <guid>https://forem.com/smartyansh/coding-best-practices-for-java-web-apps-59m2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Coding best practices are always helpful in ensuring the code quality, performance, and consistency.&lt;/p&gt;

&lt;p&gt;In this article, I'll share a few coding best practices to follow while developing Java Applications that I use in my daily coding.&lt;/p&gt;

&lt;p&gt;Before jumping to that, I'd recommend readers follow my other write-up that will set the stage for us:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/smartyansh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F237015%2F346d4930-2a19-49bf-8244-0e73f6d6f0fe.jpeg" alt="smartyansh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/smartyansh/code-review-checklist-for-java-beginners-181f" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Code Review Checklist for Java Beginners&lt;/h2&gt;
      &lt;h3&gt;Anshul Bansal ・ Jan 5 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#java&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#productivity&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#codequality&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;Now, we're all set to take a look at a few best practices:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Follow the OOPs principles&lt;/strong&gt; - &lt;em&gt;Abstraction&lt;/em&gt;, &lt;em&gt;Encapsulation&lt;/em&gt;, &lt;em&gt;Inheritance&lt;/em&gt;, and &lt;em&gt;Polymorphism&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Design Patterns&lt;/strong&gt; like &lt;em&gt;Singleton&lt;/em&gt;, &lt;em&gt;Decorator&lt;/em&gt;, &lt;em&gt;Factory&lt;/em&gt;, and &lt;em&gt;Builder-Factory&lt;/em&gt; for reusable, robust, and maintainable code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leverage the &lt;strong&gt;&lt;em&gt;Project Lombok library&lt;/em&gt; to reduce boiler-plate code&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;&lt;em&gt;Java Stream APIs&lt;/em&gt; and &lt;em&gt;Lambda expressions&lt;/em&gt; to process collections of objects&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leverage &lt;strong&gt;&lt;em&gt;Lambda expressions&lt;/em&gt; to perform functional programming in Java&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leverage &lt;strong&gt;&lt;em&gt;Generics&lt;/em&gt; for cleaner code and less duplicity&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the &lt;strong&gt;&lt;em&gt;Collections class&lt;/em&gt; for performing general operations on a collection&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the &lt;strong&gt;&lt;em&gt;LocalDate&lt;/em&gt;, &lt;em&gt;LocalTime&lt;/em&gt;, and &lt;em&gt;LocalDateTime&lt;/em&gt; classes to work with Date/Time&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consider &lt;strong&gt;&lt;em&gt;Optional&lt;/em&gt; in place of null&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prefer &lt;strong&gt;Atomic variables and Concurrent Collections over Synchronization&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Proper &lt;strong&gt;Error/Exception handling&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leverage &lt;strong&gt;ExecutorService for Asynchronous operations&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Move the &lt;strong&gt;business logic to Services&lt;/strong&gt; for reusability&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keep &lt;strong&gt;controllers thin&lt;/strong&gt; - only for handling request/response&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's a better idea to &lt;strong&gt;reduce Database interactions&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If using &lt;strong&gt;ORM technology like Hibernate&lt;/strong&gt;, use &lt;strong&gt;Lazy/Eager&lt;/strong&gt; fetching techniques wisely&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use a &lt;strong&gt;database connection pool for the handling of DB connections&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leverage &lt;strong&gt;batch insert/update for bulk transactions&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;May use &lt;strong&gt;asynchronous programming for long fetched queries&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Try &lt;strong&gt;not to block the main thread for long execution task&lt;/strong&gt; - try asynchronous programming instead using the &lt;em&gt;FutureTask&lt;/em&gt; and &lt;em&gt;CompletableFuture&lt;/em&gt; classes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enable &lt;strong&gt;logging using &lt;em&gt;SLF4J&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unit test the code using testing frameworks like &lt;em&gt;JUnit&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Document API specifications using &lt;em&gt;Swagger 2&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this short article, we've seen a few pointers for coding best practices as well as a few tips and tricks to follow while developing Java Applications.&lt;/p&gt;

&lt;p&gt;Please let me know your thoughts and share the best practices you follow in the comments below.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>java</category>
      <category>codequality</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>My Programming Journey to Senior Programmer</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Sun, 18 Oct 2020 15:03:29 +0000</pubDate>
      <link>https://forem.com/smartyansh/what-all-i-ve-tried-in-my-8-years-of-programming-544c</link>
      <guid>https://forem.com/smartyansh/what-all-i-ve-tried-in-my-8-years-of-programming-544c</guid>
      <description>&lt;h1&gt;
  
  
  1. Overview
&lt;/h1&gt;

&lt;p&gt;In this article, I'll speak about a list of things I tried or explored in my eight-plus years of programming.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What worked for me and what I've left on the way. What I'll continue and what I want to start in the future.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;My journey as a Software Programmer started when I joined a world-renowned software services provider in 2012 as a Java - Full Stack Engineer Trainee.&lt;/p&gt;

&lt;p&gt;Since then, I've tried numerous ways to learn, grow, and keeping myself updated/groomed in this rapidly changing industry.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Technical Books, Tutorials, Blogs, and Documentation
&lt;/h1&gt;

&lt;p&gt;I gathered most of my knowledge, be it basics or hands-on, of programming, technologies, and frameworks through books and tutorials.&lt;/p&gt;

&lt;p&gt;For example, books like &lt;a href="https://www.oreilly.com/library/view/head-first-java/0596009208/"&gt;Head First Java&lt;/a&gt;, &lt;a href="https://www.manning.com/books/grails-in-action"&gt;Grails in Action&lt;/a&gt;, and &lt;a href="https://www.manning.com/books/groovy-in-action-second-edition"&gt;Groovy in Action&lt;/a&gt;, tutorials like &lt;a href="https://www.tutorialspoint.com/index.htm"&gt;tutorialspoint&lt;/a&gt;, &lt;a href="https://www.w3schools.com/"&gt;w3schools&lt;/a&gt; helped a lot initially.&lt;/p&gt;

&lt;p&gt;Also, I followed a few blogs like &lt;a href="https://javarevisited.blogspot.com/"&gt;javarevisited&lt;/a&gt;, &lt;a href="https://dzone.com/"&gt;dzone&lt;/a&gt;, &lt;a href="https://www.baeldung.com/"&gt;baeldung&lt;/a&gt;, and &lt;a href="https://blog.mrhaki.com/"&gt;mrhaki blog&lt;/a&gt; to learn core/advanced features of various technologies.&lt;/p&gt;

&lt;p&gt;Then, the official documentation of the popular libraries/ frameworks like &lt;a href="https://api.jquery.com/"&gt;jQuery&lt;/a&gt;, &lt;a href="https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#spring-core"&gt;Spring&lt;/a&gt;, &lt;a href="https://docs.grails.org/4.0.4/guide/single.html"&gt;Grails&lt;/a&gt;, and &lt;a href="https://hibernate.org/orm/documentation/5.4/"&gt;Hibernate&lt;/a&gt; was a big help.&lt;/p&gt;

&lt;p&gt;Also, from the start, &lt;em&gt;I committed to myself to get hands-on with any technology or skill in just two weeks using any of the resources discussed above.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Of course, mastering something is a long-time process. However, two weeks are enough to get started, be a little productive in our work, and reduce the learning curve to a modest time-frame.&lt;/p&gt;

&lt;p&gt;It helped me a lot in all these years and will keep it going.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Presentations and Demo Sessions
&lt;/h1&gt;

&lt;p&gt;I like to attend/host technical presentations &amp;amp; demo sessions. They give a quick overview of the technology/software and helps in catching up with the updates in the industry.&lt;/p&gt;

&lt;p&gt;Also, most of the IT companies have learning programs that include webinars, sessions, and presentations that help the employees keeping themselves updated.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If there is a skill that we are good at, we should share it with others for collective growth.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For instance, I learned a great deal of Code Quality and Best practices from my seniors, design patterns, and technologies from colleagues, and the list can go on and on.&lt;/p&gt;

&lt;p&gt;Similarly, I've hosted many hands-on sessions and presentations for various frameworks and libraries.&lt;/p&gt;

&lt;p&gt;Therefore, &lt;strong&gt;to keep up with a new version/feature of technology or to learn a skill, it could be a good idea to prepare a quick presentation or hands-on project&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I believe this is something that we should do more and will keep in the loop.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Requirement Analysis
&lt;/h1&gt;

&lt;p&gt;As my career progressed, there was a need to start analyzing new projects/features based on technical and business requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quickly I learned, it's always a good idea to analyze the product offering (software) from the end-user and client's perspective.&lt;/strong&gt; In the long run, this helped me to become a better problem solver and grew as a leader.&lt;/p&gt;

&lt;p&gt;It's an ever-growing process, and we should follow it with due diligence.&lt;/p&gt;

&lt;h1&gt;
  
  
  5. Stackoverflow
&lt;/h1&gt;

&lt;p&gt;Without a doubt, I can say, all of us use StackOverflow for all our code queries or technical problems.&lt;/p&gt;

&lt;p&gt;Then, there was a phase where &lt;a href="https://stackoverflow.com/users/5440095/anshul"&gt;I started posting answers on the StackOverflow&lt;/a&gt;. It worked wonders in improving my detailed knowledge of the subject (framework/technology), debugging, and problem-solving skills.&lt;/p&gt;

&lt;p&gt;However, I couldn't continue my stint as a StackOverflow contributor for long. To follow a question and answer it with the best possible solution needed a lot of time and commitment.&lt;/p&gt;

&lt;h1&gt;
  
  
  6. Online Coding Problems
&lt;/h1&gt;

&lt;p&gt;Online coding problems provide a great way to come out of our comfort zone and challenge ourselves with broader categories of the problem - varying from technical to real-life scenarios.&lt;/p&gt;

&lt;p&gt;Around 5 years back, I started using a few platforms like &lt;a href="https://www.codingame.com/"&gt;codingame&lt;/a&gt;, &lt;a href="https://www.hackerrank.com/"&gt;hackerrank&lt;/a&gt;, and &lt;a href="https://www.hackerearth.com/"&gt;hackerearth&lt;/a&gt; for solving online coding problems.&lt;/p&gt;

&lt;p&gt;Initially, I started to enjoy the journey. Then, I got obsessed with finding solutions. There was a time when I couldn't sleep well at night without finding a solution and code it through. Soon, I pitched the idea of organizing online coding battles in my company.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solving coding problems helped all of us in improving problem-solving skills and developing the much-needed ability to follow the requirements in minute details.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Also, I was able to clear interviews with various companies, thanks to this habit.&lt;/p&gt;

&lt;p&gt;However, now I couldn't spare much time to enjoy this activity more, but this is something that I'll continue.&lt;/p&gt;

&lt;h1&gt;
  
  
  7. Personal Projects and GitHub
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Personal projects are a must for any budding programmer. Start with small programming sessions and gradually pick up to code through real-world problems.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my initial days of programming, I started working on a social networking site using the Grails framework and Groovy technology. &lt;/p&gt;

&lt;p&gt;I followed Grails in Action book to get the programming started. Along the way, I developed quite a good understanding of the Grails framework and architecture behind such complex social networking websites.&lt;/p&gt;

&lt;p&gt;The learnings always come in handy when brainstorming for solutions behind tricky problems.&lt;/p&gt;

&lt;p&gt;Not to mention, it's always a good idea to &lt;a href="https://github.com/SmartyAnsh"&gt;keep your code in Github&lt;/a&gt; or any public repository.&lt;/p&gt;

&lt;p&gt;Not only it gives you a benefit to access it from anywhere, but &lt;strong&gt;it also improves your socio-technical profile, by which your future employer can take a look at your skills and shows interest in you&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It happens to me on many occasions that companies/managers shown interest in my Github profile by checking out my repositories.&lt;/p&gt;

&lt;p&gt;Also, don't forget to add README files with setup and info about projects.&lt;/p&gt;

&lt;p&gt;Try following industry standards in writing the code to have better readability and easy to follow code. &lt;strong&gt;Having a habit of writing code following best practices helps in the long run.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  8. Client Proposals
&lt;/h1&gt;

&lt;p&gt;As I grew in my career, my seniors insisted on participating in the discussions for various technical proposals/offering to the clients.&lt;/p&gt;

&lt;p&gt;It was all about leveraging our talents to come up with the best possible solutions (within the cost) for customer satisfaction.&lt;/p&gt;

&lt;p&gt;This kind of thing can be a good brain teaser and helps to understand the different needs of the client. With such brainstorming sessions, we can become a solution provider at various levels.&lt;/p&gt;

&lt;h1&gt;
  
  
  9. Writing Technical Content
&lt;/h1&gt;

&lt;p&gt;In 2017, I attended a session on writing a technical book. &lt;/p&gt;

&lt;p&gt;It opened a different world to me. I never thought of the diligence and passion required to write a technical book.&lt;/p&gt;

&lt;p&gt;I always wanted to share my knowledge through various channels - writing technical content is one of them.&lt;/p&gt;

&lt;p&gt;Two years ago, I came across an opportunity to write technical content for Baeldung. It improved my writing skills and provided me the opportunity to gather basic knowledge/hands-on of various technologies.&lt;/p&gt;

&lt;p&gt;Since then, &lt;a href="https://www.baeldung.com/author/anshulbansal/"&gt;I wrote a handful of articles for the Baeldung&lt;/a&gt;. At the same time, &lt;a href="https://dev.to/smartyansh"&gt;I tried posting technical content on dev.to&lt;/a&gt; once in a while.&lt;/p&gt;

&lt;p&gt;Writing technical content is a big commitment. However, this is something that surely I'll keep myself hooked on.&lt;/p&gt;

&lt;h1&gt;
  
  
  10. Thinking of an Idea for a Startup
&lt;/h1&gt;

&lt;p&gt;I try to find and still finding a few problems or business ideas that can be solved using technology or a business process.&lt;/p&gt;

&lt;p&gt;Not only this activity can make us a better observer, but this also has the potential to turn out a big thing in the future. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;As we all know, everything in this industry started with an idea!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not to mention, it's an ongoing process, and I'll continue doing it.&lt;/p&gt;

&lt;h1&gt;
  
  
  11. Finding Scope of Improvement
&lt;/h1&gt;

&lt;p&gt;In everything I do, I know there is a scope for improvement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Once my manager said that if you don't find any scope of improvement in your six months old code, then you haven't learned anything for six months.&lt;/strong&gt; I truly believe in this.&lt;/p&gt;

&lt;p&gt;As we grow and evolve in the industry, we learn new techniques, new principles, and improved ways of solving a problem. &lt;/p&gt;

&lt;p&gt;For me, it's the core trait to be successful in this industry.&lt;/p&gt;

&lt;h1&gt;
  
  
  12. Things I Want to Do More and Try in Future
&lt;/h1&gt;

&lt;p&gt;Thinking of an idea/business process is going to be continued. Other than usual, I want to develop an aptitude to think of the unthinkable.&lt;/p&gt;

&lt;p&gt;Also, I want to be a contributor to open-source projects. As we all know, open-source software is so popular and is a thing of today's times.&lt;/p&gt;

&lt;p&gt;At times, I freelanced to design/implement websites, but I should explore freelancing more and want to give more time to this activity in the future.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In simple language, I want to keep exploring, improving, and enjoying my programming.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  13. Conclusion
&lt;/h1&gt;

&lt;p&gt;In this article, I took you through a series of activities that I tried over my 8+ years of programming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Many of them are very handy for most of the programmers. A few add charm to our socio-technical profile. And rest is how we want to enjoy working in this glorious industry.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'm not telling you which ones to pick - you can decide what you would want to do and what you don't.&lt;/p&gt;

&lt;p&gt;Feel free to share what worked for you and what not, in the comments. Thanks for reading. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>productivity</category>
      <category>programming</category>
    </item>
    <item>
      <title>Code Review Checklist for Java Beginners</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Sun, 05 Jan 2020 13:07:37 +0000</pubDate>
      <link>https://forem.com/smartyansh/code-review-checklist-for-java-beginners-181f</link>
      <guid>https://forem.com/smartyansh/code-review-checklist-for-java-beginners-181f</guid>
      <description>&lt;p&gt;Checklists are always helpful! They provide a quick check to ensure consistency and completeness in carrying out a task efficiently and effectively.&lt;/p&gt;

&lt;p&gt;Here, I've consolidated a basic 20 points checklist for Java Beginners to review the code. It'll help them to ensure code quality and consistency.&lt;/p&gt;

&lt;p&gt;Without further ado... Let's go through it...&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Null Checks
&lt;/h2&gt;

&lt;p&gt;We know &lt;em&gt;NullPointerException&lt;/em&gt; is the most common exception in Java and can cause big problems.&lt;/p&gt;

&lt;p&gt;So, as a general practice, always do a null check on a variable before any operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Exception Handling
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;try-catch&lt;/em&gt; block should be used for exception handling with proper logging in the &lt;em&gt;catch&lt;/em&gt; block.&lt;/p&gt;

&lt;p&gt;Also, make sure to close the resources properly in the &lt;em&gt;finally&lt;/em&gt; block.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Code Indentation and Formatting
&lt;/h2&gt;

&lt;p&gt;For a cleaner and readable code, use code indentation thoroughly (with &lt;em&gt;Tab&lt;/em&gt; or &lt;em&gt;Spaces&lt;/em&gt; anything).&lt;/p&gt;

&lt;p&gt;It can be done automatically with the built-in editor of the IDE. For instance, use &lt;em&gt;Ctrl-Shift-F&lt;/em&gt; in Eclipse. Similarly, &lt;em&gt;Ctrl-Alt-L&lt;/em&gt; in IntelliJ.&lt;/p&gt;

&lt;p&gt;Make use of Java formatting rules!&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Optimize Imports
&lt;/h2&gt;

&lt;p&gt;Always optimize imports in the Java class.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Static Code Review Tools
&lt;/h2&gt;

&lt;p&gt;Use static code review tools like Sonar, PMD, and FindBugs to review the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Constants
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a constant file for static values that are needed at multiple places &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Database-driven values for dynamic values&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use ENUMs for a group of constants&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Naming Conventions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Always check if the name of a variable/method/class truly covers the subject&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Package names should be in all lower cases that start with reversed Internet domain name followed by application name. For example, &lt;em&gt;org/companyname/appname&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Class names should start with Capitals. For instance, &lt;em&gt;Animal&lt;/em&gt;, &lt;em&gt;Employee&lt;/em&gt;, and &lt;em&gt;User&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variable/Method names should be in CamelCase. For instance &lt;em&gt;animalInstanceList&lt;/em&gt;, &lt;em&gt;calculateAmount&lt;/em&gt;, and &lt;em&gt;displaySummary()&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Try to avoid abbreviations in class/method/variable names. Prefer &lt;em&gt;domainCode&lt;/em&gt; over &lt;em&gt;dmnCd&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Not All One-Liners
&lt;/h2&gt;

&lt;p&gt;It's good to keep the code clean and readable. So, it's a better idea to not always go with one-liners. Especially, when we initialize and operate the variable in one line.&lt;/p&gt;

&lt;p&gt;For example, write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"offset"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"-"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"count"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"offset"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"max"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"count"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"offset"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"count"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"max"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"-"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  9. White-Spaces
&lt;/h2&gt;

&lt;p&gt;Use white-spaces to separate combined statements to make code more readable.&lt;/p&gt;

&lt;p&gt;For example, write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;offset&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;offset&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;offset&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;offset&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;offset&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;offset&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10. Spaces Before and After Brackets
&lt;/h2&gt;

&lt;p&gt;In general, we don't use white spaces in the brackets.&lt;/p&gt;

&lt;p&gt;For example, write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  11. Curly Braces
&lt;/h2&gt;

&lt;p&gt;Use curly braces for one-liners also.&lt;/p&gt;

&lt;p&gt;For example, write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  12. Comments
&lt;/h2&gt;

&lt;p&gt;Always put comments (if any) defining the purpose.&lt;/p&gt;

&lt;p&gt;For example, Javadoc on a class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * General convenience tags for layout - header, body and footer
 * @author – Name
 * @dateCreated - Date
 */&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LayoutTagLib&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Javadoc on a method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Gets the user for specified code and role.
 * @param code :- The code, either username or email address
 * @param role :- The role identification e.g. A, B or C. Default is A.
 * @return the user or null if not found
 */&lt;/span&gt;
&lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nf"&gt;findUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure the code is self-explanatory and comments are really useful in very specific cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  13. Clean Up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Remove console print Statements (SOPs), use logging instead (never log personal information)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remove obsolete comments&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/deprecated"&gt;@deprecated&lt;/a&gt;&lt;/em&gt; annotation on the method/variable, if it is not meant for future use or going to be removed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remove hardcoded variable values&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  14. Business Logic
&lt;/h2&gt;

&lt;p&gt;Avoid redundant code by using reusable components like utilities and service methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  15. &lt;em&gt;StringBuilder&lt;/em&gt; or &lt;em&gt;StringBuffer&lt;/em&gt; in Place of &lt;em&gt;String&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;When performing a lot of operations on the &lt;em&gt;String&lt;/em&gt;, use &lt;em&gt;StringBuilder&lt;/em&gt; or &lt;em&gt;StringBuffer&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;For example, Java creates a new &lt;em&gt;String&lt;/em&gt; object for every concatenation operation. In this case, a better idea is to use a &lt;em&gt;StringBuffer&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  16. &lt;em&gt;switch-case&lt;/em&gt; Over Multiple &lt;em&gt;if-else&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;It's a good practice to use &lt;em&gt;switch-case&lt;/em&gt; in place of multiple &lt;em&gt;if-else&lt;/em&gt; conditions. &lt;/p&gt;

&lt;p&gt;It optimizes the code execution and also makes code cleaner and more readable.&lt;/p&gt;

&lt;h2&gt;
  
  
  17. Objects Creation in a Loop
&lt;/h2&gt;

&lt;p&gt;It is usually better to create the object inside the loop (If object is not required outside loop). Java optimizes memory usage for short-lived objects.&lt;/p&gt;

&lt;p&gt;For example, write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, create a new object only if required.&lt;/p&gt;

&lt;p&gt;For example, write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;personList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;personList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;personList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;namesList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;personList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  18. Code Commit
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Group the files and commit together (don't commit files in separate commits)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don't commit the code which has the actual Password. Make sure to use a system/configuration variable to replace the password&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Commit messages should contain the task information. For example, JIRA issue number, a meaningful comment on code implementation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Commit &lt;em&gt;.class&lt;/em&gt; files (from the &lt;em&gt;build&lt;/em&gt;, &lt;em&gt;out&lt;/em&gt;, &lt;em&gt;target&lt;/em&gt; directories), only if required&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  19. Use &lt;em&gt;equals&lt;/em&gt; over ==
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;equals&lt;/em&gt; perform the actual comparison of two strings, whereas == compares object references.&lt;/p&gt;

&lt;h2&gt;
  
  
  20. Keep It Simple!
&lt;/h2&gt;

&lt;p&gt;Maintain simplicity and readability of code.&lt;/p&gt;

&lt;p&gt;Please let me know your thoughts on it.&lt;br&gt;
Thanks for reading.&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>productivity</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Create RESTful APIs using Spring Boot + Mongo DB</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Mon, 30 Dec 2019 15:36:25 +0000</pubDate>
      <link>https://forem.com/smartyansh/create-restful-apis-using-spring-boot-mongo-db-6cf</link>
      <guid>https://forem.com/smartyansh/create-restful-apis-using-spring-boot-mongo-db-6cf</guid>
      <description>&lt;h2&gt;
  
  
  1. Overview
&lt;/h2&gt;

&lt;p&gt;Nowadays, a web application is often separated into two components - frontend and backend.&lt;/p&gt;

&lt;p&gt;With the growing popularity of frontend technologies like Angular and React, backend features are often exposed as the RESTful APIs.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll create RESTful APIs using &lt;a href="https://spring.io/projects/spring-boot"&gt;Spring Boot&lt;/a&gt;. At the same time, we'll use &lt;a href="https://spring.io/projects/spring-data-mongodb"&gt;Spring Data MongoDB&lt;/a&gt; to provide persistence support in our App by connecting to MongoDB.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Prerequisites
&lt;/h2&gt;

&lt;p&gt;We need a set of standard tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Java 8 or later&lt;/li&gt;
&lt;li&gt;Build Tool (Maven or Gradle)&lt;/li&gt;
&lt;li&gt;IDE (IntelliJ or Eclipse)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll use a combination of Java 8, &lt;a href="https://gradle.org/"&gt;Gradle&lt;/a&gt; and &lt;a href="https://www.jetbrains.com/idea/"&gt;IntelliJ&lt;/a&gt; for our implementation.&lt;/p&gt;

&lt;p&gt;To quickly create the Spring Boot application, we'll use &lt;a href="https://start.spring.io/"&gt;Spring Initializr&lt;/a&gt;. Also, we'll install and configure &lt;a href="https://docs.mongodb.com/manual/installation/"&gt;Mongo DB&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Application
&lt;/h2&gt;

&lt;p&gt;Let's create RESTful APIs for a user registration app. &lt;br&gt;
For simplicity, we'll just add features to perform CRUD (Create, Read, Update and Delete) operations.&lt;/p&gt;

&lt;p&gt;Once the Spring Boot app is created, we'll create the &lt;em&gt;application.properties&lt;/em&gt; file with MongoDB configuration details:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.data.mongodb.database=springboot_mongo_db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.1. Entity
&lt;/h3&gt;

&lt;p&gt;First, we'll create the &lt;em&gt;User&lt;/em&gt; entity with basic properties like &lt;em&gt;firstName&lt;/em&gt;, &lt;em&gt;lastName&lt;/em&gt;, and &lt;em&gt;email&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Getter&lt;/span&gt;
&lt;span class="nd"&gt;@Setter&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we've used &lt;a href="https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/Id.html"&gt;&lt;em&gt;@Id&lt;/em&gt;&lt;/a&gt; annotation to define the identifier.&lt;/p&gt;

&lt;p&gt;At the same time, we've used &lt;a href="https://projectlombok.org/api/lombok/Getter.html"&gt;&lt;em&gt;@Getter&lt;/em&gt;&lt;/a&gt; and &lt;a href="https://projectlombok.org/api/lombok/Setter.html"&gt;&lt;em&gt;@Setter&lt;/em&gt;&lt;/a&gt; annotations provided by the &lt;a href="https://objectcomputing.com/resources/publications/sett/january-2010-reducing-boilerplate-code-with-project-lombok"&gt;Project Lombok&lt;/a&gt; to create getters/setters automatically.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2. Repository
&lt;/h3&gt;

&lt;p&gt;Then, we'll create the &lt;em&gt;UserRepository&lt;/em&gt; interface that provides basic operations on the database using Spring Data provided &lt;a href="https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/repository/MongoRepository.html"&gt;&lt;em&gt;MongoRepository&lt;/em&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MongoRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.3. Service
&lt;/h3&gt;

&lt;p&gt;Next, we'll create a service class that provides the business logic for basic CRUD operations. &lt;/p&gt;

&lt;p&gt;For this, let's create the &lt;em&gt;Registration&lt;/em&gt; interface to define the abstraction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;USER&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;USER&lt;/span&gt; &lt;span class="nf"&gt;registerUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;USER&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="no"&gt;USER&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ID&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="no"&gt;USER&lt;/span&gt; &lt;span class="nf"&gt;updateUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;USER&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;deleteUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ID&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we'll create the &lt;em&gt;RegistrationImpl&lt;/em&gt; class to implement the &lt;em&gt;Registration&lt;/em&gt; interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RegistrationImpl&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nf"&gt;registerUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getAllUser&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findAll&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nf"&gt;updateUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;deleteUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"user id is null"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we've used &lt;a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/Service.html"&gt;&lt;em&gt;@Service&lt;/em&gt;&lt;/a&gt; annotation to declare the &lt;em&gt;RegistrationImpl&lt;/em&gt; class as a service for Spring Boot. Also, we've used &lt;a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html"&gt;&lt;em&gt;@Autowired&lt;/em&gt;&lt;/a&gt; annotation to inject the &lt;em&gt;UserRepository&lt;/em&gt; dependency.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.4. Controller
&lt;/h3&gt;

&lt;p&gt;Now that we've got entity, repository and service ready, we'll create the &lt;em&gt;RegistrationController&lt;/em&gt; class to expose the APIs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RegistrationController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;RegistrationImpl&lt;/span&gt; &lt;span class="n"&gt;registrationImpl&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we've used &lt;a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html"&gt;&lt;em&gt;@RestController&lt;/em&gt;&lt;/a&gt; annotation to declare it as a RESTful API controller.&lt;/p&gt;

&lt;p&gt;At first, we'll add a method &lt;em&gt;registerUser&lt;/em&gt; to register the user (CREATE):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/registerUser"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt; &lt;span class="nf"&gt;registerUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="n"&gt;registrationImpl&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;registerUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"user"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voila, we're ready with our first RESTful API to register the user.&lt;/p&gt;

&lt;p&gt;Let's try it using the following curl command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST &lt;span class="s2"&gt;"http://localhost:8080/registerUser"&lt;/span&gt; &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{"email": "xyz@email.com", "firstName": "norman", "lastName": "lewis", "phone": "90909090"}'&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, let's add a method &lt;em&gt;getUser&lt;/em&gt; to search the user by id (READ):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/getUser"&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;registrationImpl&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, we can use the curl to read the user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; GET &lt;span class="s2"&gt;"http://localhost:8080/getUser?id=5e0a0a393844b64a1548c4d9"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, we can define a method &lt;em&gt;getAllUser&lt;/em&gt; to fetch all the users:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/getAllUser"&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt; &lt;span class="nf"&gt;getAllUser&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;registrationImpl&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAllUser&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we'll add a method &lt;em&gt;updateUser&lt;/em&gt; to update the user (UPDATE):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@PutMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/updateUser"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt; &lt;span class="nf"&gt;updateUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
&lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="n"&gt;registrationImpl&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;updateUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"user"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last, let's add a method &lt;em&gt;deleteUser&lt;/em&gt; to delete the user (DELETE):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@DeleteMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/deleteUser"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt; &lt;span class="nf"&gt;deleteUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;registrationImpl&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deleteUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"User is successfully deleted"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.5. Validation
&lt;/h3&gt;

&lt;p&gt;Next, we'll add the feature of bean validations to our APIs using &lt;a href="https://hibernate.org/validator/"&gt;&lt;em&gt;hibernate-validator&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;First, we'll add the dependency to the &lt;em&gt;build.gradle&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="nl"&gt;group:&lt;/span&gt; &lt;span class="s1"&gt;'org.hibernate'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;name:&lt;/span&gt; &lt;span class="s1"&gt;'hibernate-validator'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;version:&lt;/span&gt; &lt;span class="s1"&gt;'5.0.0.Final'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we'll add a few annotations like &lt;a href="https://docs.oracle.com/javaee/7/api/javax/validation/constraints/NotNull.html"&gt;&lt;em&gt;@NotNull&lt;/em&gt;&lt;/a&gt; and &lt;a href="https://docs.jboss.org/hibernate/validator/6.0/api/index.html?org/hibernate/validator/constraints/Email.html"&gt;&lt;em&gt;@Email&lt;/em&gt;&lt;/a&gt; to the &lt;em&gt;User&lt;/em&gt; entity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="nd"&gt;@NotNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"First Name is mandatory"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@NotNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Last Name is mandatory"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@NotNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Email is mandatory"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@Email&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Not valid email"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@Indexed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unique&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@NotNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Phone Number is mandatory"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we'll use the &lt;a href="https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/mapping/event/ValidatingMongoEventListener.html"&gt;&lt;em&gt;ValidatingMongoEventListener&lt;/em&gt;&lt;/a&gt; to register as a bean in the main class - &lt;em&gt;Application&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ValidatingMongoEventListener&lt;/span&gt; &lt;span class="nf"&gt;validatingMongoEventListener&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ValidatingMongoEventListener&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;validator&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;LocalValidatorFactoryBean&lt;/span&gt; &lt;span class="nf"&gt;validator&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;LocalValidatorFactoryBean&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last, we'll modify the &lt;em&gt;registerUser&lt;/em&gt; method in the &lt;em&gt;RegistrationController&lt;/em&gt; to validate the User object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/registerUser"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt; &lt;span class="nf"&gt;registerUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;registrationImpl&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;registerUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ConstraintViolationException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getConstraintViolations&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;forEach&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;constraintViolation&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;constraintViolation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPropertyPath&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
                    &lt;span class="n"&gt;constraintViolation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="o"&gt;});&lt;/span&gt;
        &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"messages"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"user"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! We're ready to examine the validation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST &lt;span class="s2"&gt;"http://localhost:8080/registerUser"&lt;/span&gt; &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{"email": "xyz@email.com"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we've not provided &lt;em&gt;firstName&lt;/em&gt;, &lt;em&gt;lastName&lt;/em&gt;, and &lt;em&gt;phone&lt;/em&gt;. So, the response will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"messages"&lt;/span&gt;:&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"firstName"&lt;/span&gt;:&lt;span class="s2"&gt;"First Name is mandatory"&lt;/span&gt;,&lt;span class="s2"&gt;"lastName"&lt;/span&gt;:&lt;span class="s2"&gt;"Last Name is mandatory"&lt;/span&gt;,&lt;span class="s2"&gt;"phone"&lt;/span&gt;:&lt;span class="s2"&gt;"Phone Number is mandatory"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;,&lt;span class="s2"&gt;"error"&lt;/span&gt;:true,&lt;span class="s2"&gt;"user"&lt;/span&gt;:&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;:null,&lt;span class="s2"&gt;"firstName"&lt;/span&gt;:null,&lt;span class="s2"&gt;"lastName"&lt;/span&gt;:null,&lt;span class="s2"&gt;"email"&lt;/span&gt;:&lt;span class="s2"&gt;"xyz12@email.com"&lt;/span&gt;,&lt;span class="s2"&gt;"phone"&lt;/span&gt;:null,&lt;span class="s2"&gt;"address"&lt;/span&gt;:null&lt;span class="o"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.6. Swagger Integration
&lt;/h3&gt;

&lt;p&gt;So far, we've used curl to request our APIs. Now, we'll integrate &lt;a href="https://dev.to/smartyansh/introduction-to-springfox-48ng"&gt;Swagger 2 documentation for our APIs&lt;/a&gt; using Springfox.&lt;/p&gt;

&lt;p&gt;All we need is to add the Springfox dependencies in &lt;em&gt;build.gradle&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="s2"&gt;"io.springfox:springfox-swagger2:2.9.2"&lt;/span&gt;
&lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="s1"&gt;'io.springfox:springfox-swagger-ui:2.9.2'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we'll annotate the main class with @EnableSwagger2 annotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@EnableSwagger2&lt;/span&gt;
&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Our API specifications are ready and are accessible at &lt;em&gt;&lt;a href="http://localhost:8080/swagger-ui.html"&gt;http://localhost:8080/swagger-ui.html&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Conclusion
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we've created RESTful APIs for the User registration App using SpringBoot and Spring Data Mongo DB.&lt;br&gt;
Also, we've used Project Lombok to reduce the boiler-plate code and Springfox APIs for Swagger 2 documentation.&lt;/p&gt;

&lt;p&gt;The entire code implementations are available over on &lt;a href="https://github.com/SmartyAnsh/springboot_mongo"&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please let me know your thoughts on it!&lt;br&gt;
Thanks for reading.&lt;/p&gt;

</description>
      <category>java</category>
      <category>tutorial</category>
      <category>mongodb</category>
      <category>springboot</category>
    </item>
    <item>
      <title>Swagger-2 API documentation for Spring App using Springfox</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Mon, 16 Dec 2019 21:58:00 +0000</pubDate>
      <link>https://forem.com/smartyansh/introduction-to-springfox-48ng</link>
      <guid>https://forem.com/smartyansh/introduction-to-springfox-48ng</guid>
      <description>&lt;h2&gt;
  
  
  1. Overview
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://spring.io/" rel="noopener noreferrer"&gt;Spring&lt;/a&gt; is a very popular JEE framework to create web applications. &lt;/p&gt;

&lt;p&gt;Often, we use Spring technologies to expose APIs to third-party integrations.  At that time, our APIs require a detailed specification that helps in easy integrations.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll explore the Springfox Java libraries to generate &lt;a href="https://swagger.io/" rel="noopener noreferrer"&gt;Swagger&lt;/a&gt; based API specifications for the Spring application.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Springfox
&lt;/h2&gt;

&lt;p&gt;Springfox is a set of Java libraries, that has evolved from the &lt;a href="https://search.maven.org/search?q=g:com.mangofactory%20a:swagger-springmvc" rel="noopener noreferrer"&gt;&lt;em&gt;swagger-springmvc&lt;/em&gt;&lt;/a&gt; project.&lt;/p&gt;

&lt;p&gt;It automates the generation of specifications for JSON APIs, implemented with the Spring framework. Also, it provides libraries to integrate the Swagger UI to interact with APIs.&lt;/p&gt;

&lt;p&gt;Springfox examines the Spring application at runtime and generates the API specifications based on configurations and annotations.&lt;/p&gt;

&lt;p&gt;Let's explore &lt;a href="https://swagger.io/docs/specification/2-0/basic-structure/" rel="noopener noreferrer"&gt;Swagger 2&lt;/a&gt; integration with Spring REST API. Also, we'll touch upon the basic configurations.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Setup
&lt;/h2&gt;

&lt;p&gt;Let's add the latest &lt;a href="https://search.maven.org/search?q=g:io.springfox%20a:springfox-swagger2" rel="noopener noreferrer"&gt;&lt;em&gt;springfox-swagger2&lt;/em&gt;&lt;/a&gt; Maven dependency to our Spring Boot application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;io.springfox&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;springfox-swagger2&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;2.9.2&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, we can add the &lt;em&gt;springfox-swagger2&lt;/em&gt; dependency to a Gradle based Spring project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="s2"&gt;"io.springfox:springfox-swagger2:2.9.2"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. &lt;em&gt;@EnableSwagger2&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we've set up the Springfox dependency. Let's enable Swagger 2 to generate the API specifications.&lt;/p&gt;

&lt;p&gt;To do so, we'll add the &lt;a href="http://springfox.github.io/springfox/javadoc/current/index.html?springfox/documentation/swagger2/annotations/EnableSwagger2.html" rel="noopener noreferrer"&gt;&lt;em&gt;@EnableSwagger2&lt;/em&gt;&lt;/a&gt; annotation to the main class of our application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@EnableSwagger2&lt;/span&gt;
&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SpringfoxApplication&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SpringfoxApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's start the application with the Maven command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn spring-boot:run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voila! The API specifications are ready.&lt;/p&gt;

&lt;p&gt;As a result, we can access the specifications in the JSON format at &lt;em&gt;localhost:8080/v2/api-docs&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F27ijiht4vd8iw65swvok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F27ijiht4vd8iw65swvok.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Swagger UI
&lt;/h2&gt;

&lt;p&gt;The API specification in the JSON format is not readable and difficult to follow. As a result, we need UI support that makes interaction with the API specifications easy.&lt;/p&gt;

&lt;p&gt;Therefore, let's integrate the &lt;a href="https://swagger.io/tools/swagger-ui/" rel="noopener noreferrer"&gt;Swagger UI&lt;/a&gt; to our application, by adding the &lt;a href="https://search.maven.org/search?q=g:io.springfox%20a:springfox-swagger-ui" rel="noopener noreferrer"&gt;&lt;em&gt;springfox-swagger-ui&lt;/em&gt;&lt;/a&gt; dependency into the &lt;em&gt;pom.xml&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;io.springfox&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;springfox-swagger-ui&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;2.9.2&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we'll restart the app and access the Swagger UI at &lt;em&gt;localhost:8080/swagger-ui.html&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr1q9sd6u3rv7xxucer7i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr1q9sd6u3rv7xxucer7i.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Spring REST API
&lt;/h2&gt;

&lt;p&gt;Let's explore the Swagger 2 integration with the Spring REST API.&lt;/p&gt;

&lt;p&gt;First, let's create an entity named &lt;em&gt;User&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;//getters and setters&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we'll create the &lt;em&gt;UserRepository&lt;/em&gt; to add CRUD operations on the &lt;em&gt;User&lt;/em&gt; entity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Repository&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;CrudRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we'll create the &lt;em&gt;UserController&lt;/em&gt; for the REST APIs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/api/user"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;produces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;APPLICATION_JSON_VALUE&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nd"&gt;@ResponseBody&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nd"&gt;@ResponseBody&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestParam&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we've got the entity, repository, and controller in place. Let's integrate everything into the main class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class="nd"&gt;@EnableSwagger2&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SpringfoxApplication&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we'll restart the application to see the changes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fh7gu1ur7ft08pzzqj42x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fh7gu1ur7ft08pzzqj42x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we can see the specifications are generated for the &lt;em&gt;UserController&lt;/em&gt; APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Configuration
&lt;/h2&gt;

&lt;p&gt;Let's explore the basics of Springfox configuration.&lt;/p&gt;

&lt;p&gt;We can configure the API specifications using the &lt;a href="http://springfox.github.io/springfox/javadoc/current/springfox/documentation/spring/web/plugins/Docket.html" rel="noopener noreferrer"&gt;&lt;em&gt;Docket&lt;/em&gt;&lt;/a&gt; class. For this, we should create a method in the main class registered as a bean and return a &lt;em&gt;Docket&lt;/em&gt; instance.&lt;/p&gt;

&lt;p&gt;Let's create a &lt;em&gt;springfoxAppInfo&lt;/em&gt; bean method in the &lt;em&gt;SpringfoxApplication&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SpringfoxApplication&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;

    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Docket&lt;/span&gt; &lt;span class="nf"&gt;springfoxAppInfo&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Docket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DocumentationType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SWAGGER_2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
          &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;groupName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Springfox-api"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
          &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;select&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
          &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paths&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
          &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
          &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;apiInfo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apiInfo&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Predicate&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;paths&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;regex&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/api/.*"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;       
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ApiInfo&lt;/span&gt; &lt;span class="nf"&gt;apiInfo&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ApiInfo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
          &lt;span class="s"&gt;"Springfox API specification"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
          &lt;span class="s"&gt;"Spring REST APIs"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
          &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
          &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
          &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
          &lt;span class="s"&gt;"License of API"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
          &lt;span class="s"&gt;"API license URL"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
          &lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;emptyList&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we've changed a few properties like the &lt;em&gt;groupName&lt;/em&gt; and &lt;a href="http://springfox.github.io/springfox/javadoc/current/springfox/documentation/service/ApiInfo.html" rel="noopener noreferrer"&gt;&lt;em&gt;apiInfo&lt;/em&gt;&lt;/a&gt;. Also, we've restricted our API specifications to the &lt;em&gt;/api&lt;/em&gt; URI paths.&lt;/p&gt;

&lt;p&gt;Let's restart the application and check out the differences in the specifications:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frvkhtjgbkghdtekig4ua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frvkhtjgbkghdtekig4ua.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we can observe that the API info has changed. Also, the specification is available only for &lt;em&gt;UserController&lt;/em&gt; based on the &lt;em&gt;/api&lt;/em&gt; URI path mentioned.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we've explored the Springfox suite of libraries for generating specifications of the APIs, implemented using the Spring framework.&lt;/p&gt;

&lt;p&gt;To begin with, we've created a Spring Boot application and integrate Swagger 2 APIs to create the specifications. Also, we've seen integration with the Swagger UI.&lt;/p&gt;

&lt;p&gt;Then, we've examined ways to integrate Swagger into Spring REST APIs.&lt;/p&gt;

&lt;p&gt;Last, we've touched upon the basics of Swagger UI configuration.&lt;/p&gt;

&lt;p&gt;Please find the entire code implementations over on &lt;a href="https://github.com/SmartyAnsh/springfox" rel="noopener noreferrer"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Would love to hear your thoughts on the article. Have any questions? Any feedback? Please comment!!!&lt;/p&gt;

</description>
      <category>java</category>
      <category>tutorial</category>
      <category>spring</category>
      <category>swagger2</category>
    </item>
    <item>
      <title>Hexagonal Architecture in Java</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Sun, 15 Dec 2019 17:39:10 +0000</pubDate>
      <link>https://forem.com/smartyansh/hexagonal-architecture-in-java-4m4l</link>
      <guid>https://forem.com/smartyansh/hexagonal-architecture-in-java-4m4l</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. Overview&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this quick article, we'll discuss Hexagonal Architecture in Java through a practical example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. What is Hexagonal Architecture?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In simple words, a Hexagonal architecture separates software in two components - inside and outside, instead of conceptual layers.&lt;br&gt;
The component that remains inside usually consists of application and domain layers, along with the core business logic. Whereas, the component for the outside world consists of layers like UI and Database.&lt;/p&gt;

&lt;p&gt;The connection between the inside and outside components realizes via abstractions called ports and implementations called adapters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Benefits&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flexibility - with different adapters, the software can serve multiple channels&lt;/li&gt;
&lt;li&gt;Testability - as mocking code is easy&lt;/li&gt;
&lt;li&gt;Purity - as the core logic remains intact&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Hexagonal Architecture in Java&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Java, interfaces are ports and implementation classes are adapters.&lt;/p&gt;

&lt;p&gt;Let's create a simple Spring Boot App and try to register/view Employee details using the Hexagonal Architecture.&lt;/p&gt;

&lt;p&gt;To start with, we'll create an entity named &lt;em&gt;Employee&lt;/em&gt;.&lt;br&gt;
Then, the &lt;em&gt;EmployeeService&lt;/em&gt; class to keep the core business logic to persist data. &lt;/p&gt;

&lt;p&gt;Here, the &lt;em&gt;Employee&lt;/em&gt; and &lt;em&gt;EmployeeService&lt;/em&gt; classes are part of the inside component. Therefore, we'll create ports to expose them.&lt;/p&gt;

&lt;p&gt;First, let's create the &lt;em&gt;EmployeeUIPort&lt;/em&gt; interface to communicate with the front-end:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface EmployeeUIPort {
    @PostMapping("register")
    public void register(@RequestBody Employee request);

    @GetMapping("view/{id}")
    public Employee view(@PathVariable Integer id);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, we'll create the &lt;em&gt;EmployeeDBPort&lt;/em&gt; interface to communicate with the database:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface EmployeeDBPort {
    void register(String name);

    Employee getEmployee(Integer id);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Last, we need adapters to connect to the ports - &lt;em&gt;EmployeeUIPort&lt;/em&gt; and &lt;em&gt;EmployeeDBPort&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's create the &lt;em&gt;EmployeeControllerAdapter&lt;/em&gt; class that implements the &lt;em&gt;EmployeeUIPort&lt;/em&gt; interface and act as a &lt;em&gt;RestController&lt;/em&gt;. Therefore, it'll act as an adapter to connect with the front-end:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@RestController
@RequestMapping("/employee/")
public class EmployeeControllerAdapter implements EmployeeUIPort {
    @Autowired
    private EmployeeServiceAdapter employeeService;

    @Override
    public void register(@RequestBody Employee request) {
        employeeService.register(request.getName());
    }

    @Override
    public Employee view(@PathVariable Integer id) {
        Employee employee = employeeService.view(id);
        return employee;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, we'll create the &lt;em&gt;EmployeeServiceAdapter&lt;/em&gt; (or &lt;em&gt;EmployeeService&lt;/em&gt; for simplicity) class that implements the &lt;em&gt;EmployeeDBPort&lt;/em&gt; interface and keep it as a Spring Service. So, it'll behave as an adapter to persist data in the database:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Service
public class EmployeeServiceAdapter implements EmployeeDBPort {
    @PersistenceContext
    private EntityManager entityManager;

    @Transactional
    @Override
    public void register(String name) {
        Employee employee = new Employee();
        employee.setName(name);

        entityManager.persist(employee);
    }

    @Override
    public Employee getEmployee(Integer id) {
        return entityManager.find(Employee.class, id);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That's it, we've successfully separated our App into the inside and outside components. &lt;/p&gt;

&lt;p&gt;As a result, the business logic of the app will be exposed via ports and consumed via adapters from outside.&lt;/p&gt;

&lt;p&gt;Similarly, we can create different ports and implement multiple adapters for any core service. &lt;/p&gt;

&lt;p&gt;For instance, Messaging Service and Content Management Service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this quick article, we've explored the concept of Hexagonal architecture in Java. &lt;/p&gt;

&lt;p&gt;We've seen the core business logic (inside component) can be exposed as ports via interfaces and consumed by different adapters (outside component) through implementation classes.&lt;/p&gt;

&lt;p&gt;This approach helps in writing clear, readable code with less dependency on external technology. However, I'd suggest using this pattern of ports and adapters selectively, rather than going full hexagonal.&lt;/p&gt;

</description>
      <category>java</category>
      <category>hexagonalarchitecture</category>
      <category>programming</category>
    </item>
    <item>
      <title>Best Possible Hibernate Configuration for Batch Inserts</title>
      <dc:creator>Anshul Bansal</dc:creator>
      <pubDate>Thu, 26 Sep 2019 14:10:56 +0000</pubDate>
      <link>https://forem.com/smartyansh/best-possible-hibernate-configuration-for-batch-inserts-2a7a</link>
      <guid>https://forem.com/smartyansh/best-possible-hibernate-configuration-for-batch-inserts-2a7a</guid>
      <description>&lt;h1&gt;
  
  
  Problem
&lt;/h1&gt;

&lt;p&gt;In general, the hibernate entities (domains) are set to use database sequence as Id generator.&lt;br&gt;
In such a case, for every insert hibernate makes two round trips to the database. It'll first make a round trip to get the next value of the sequence to set the identifier of the record. Then make another round trip to insert the record.&lt;/p&gt;

&lt;p&gt;Assume, we want to insert 1000 records in the database. It'll result in a total of 2000 round trips to the database (1000 round trips each, to get the next value of a sequence and insert the records).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Even in a case of very low network latency, performing a few thousand inserts may require a significant amount of time.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The first major problem is to perform each insert separately.&lt;br&gt;
The second major problem is to make a round trip to get the next value of the sequence every time.&lt;/p&gt;
&lt;h1&gt;
  
  
  Theoretical Solution
&lt;/h1&gt;

&lt;p&gt;We should try to reduce the network round trips to the database for bulk inserts by batch processing of inserts.&lt;br&gt;
Along with that, reduce the network round trips to get the next value of the sequence for every insert.&lt;/p&gt;
&lt;h1&gt;
  
  
  Hibernate Solution
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;First problem:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We know the obvious. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Use the &lt;a href="https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#batch"&gt;JDBC batching&lt;/a&gt; provided by Hibernate.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Set the following properties in the hibernate configuration file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;property&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"hibernate.jdbc.batch_size"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"100"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;property&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"hibernate.order_inserts"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, hibernate will make a batch of 100 inserts and orders them. Then, it will make a single network round trip to the database to insert 100 records.&lt;/p&gt;

&lt;p&gt;Therefore, the initial 1000 round trips to insert the records will reduce to 10.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second problem:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll use enhance sequence identifier with an &lt;a href="https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators-optimizer"&gt;optimizer&lt;/a&gt; strategy like pooled or pooled-lo, which provides in-memory identifiers. Therefore, they reserve ids in memory to be used later.&lt;/p&gt;

&lt;p&gt;Let's see what a 'pooled' optimizer strategy can do!&lt;/p&gt;

&lt;p&gt;To enable it, we'll require to set the 'INCREMENT BY' of the database sequence to 100. &lt;/p&gt;

&lt;p&gt;Then, set the pooled optimizer strategy with &lt;em&gt;increment_size = 100&lt;/em&gt; in the entity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Id&lt;/span&gt;
&lt;span class="nd"&gt;@GenericGenerator&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"sequenceGenerator"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;strategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"org.hibernate.id.enhanced.SequenceStyleGenerator"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parameters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;@Parameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"sequence_name"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hibernate_sequence"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
        &lt;span class="nd"&gt;@Parameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"optimizer"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"pooled"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
        &lt;span class="nd"&gt;@Parameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"initial_value"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
        &lt;span class="nd"&gt;@Parameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"increment_size"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"100"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;strategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GenerationType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SEQUENCE&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;generator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"sequenceGenerator"&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, hibernate will make a round trip to the database and set the sequence nextval to next 100 value. And, reserve the 100 values in memory to set the ids for the inserts. &lt;br&gt;
With this approach, we'll make 1 round trip to fetch the next value of the sequence for every 100 records to insert.&lt;/p&gt;

&lt;p&gt;Hence, the initial 1000 round trips to get the next value of the sequence will reduce to 10.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Therefore applying both of the solutions, we can reduce the round trips to just 20 for 1000 inserts.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We all may know about batch insert optimization. But, the sequence optimizer is quite a winner.&lt;br&gt;
Not many of us know this kind of optimization strategy available already.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Isn't it sound cool and smart.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Guys, Please do share your thoughts with me.&lt;/p&gt;

</description>
      <category>database</category>
      <category>batching</category>
      <category>hibernate</category>
      <category>java</category>
    </item>
  </channel>
</rss>
