Posts

Validate model with Strategy Pattern to keep Open–Closed Principle

Image
  Defensive Programming with Strategy Pattern Defensive programming is an approach to programming that attempts to ensure that software still functions under adverse or unforeseen circumstances.  Open–Closed Principle Classes, functions, and modules should be closed for modification but open for extension. That means closed for modification and open for extension. Adding new functionality should not modify existing source code. A component should be extendable to make it behave in new ways. Why Strategy pattern? The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. 

Using Spring REST Docs with automated tests

Image
  Spring REST docs provide a lot of features and flexibility to generate documentation for your REST APIs. One of the powers of REST docs is its ability to generate the documentation based on the unit tests which ensure that documentation is accurate, concise, and well-structured. To enable the Spring REST Docs support, add spring-restdocs-mockmvc as an additional dependency in your application.  Spring REST Docs use the test cases to generate accurate and updated documentation for the REST API. If all tests pass with no issue, it will generate a sub-folder under the target/generated-snippets with a name matching the argument passed to the document method (in our case it is itShouldReturnThreeCustomers). There will be several .adoc files. One of the limitations with the AsciiDoc doc is that it is unable to render in the browser. So AsciiDoc need to be converted into a browser-friendly format like HTML. To convert our documentation into HTML format, must be use th...

Generate the documentation at runtime when app is deployed

Image
To generate the documentation at runtime, when app is deployed you need springdoc-openapi-maven-plugin .  The aim of springdoc-openapi-maven-plugin is to generate json and yaml OpenAPI description during runtime.  If you want to get swagger definitions properly, the application should completely running as locally.  The plugin works during integration-tests phase, and generate the OpenAPI description.  The plugin works in conjunction with spring-boot-maven plugin.  You can test it during the integration tests phase using the maven command: mvn verify   In order to use this functionality, you need to add the plugin declaration on the plugins section of your pom.xml. References:  springdoc-openapi-maven-plugin

Documenting a Spring REST API Using OpenAPI 3.0

Image
Why API Documentation Matters? API documentation is human and machine-readable technical content that explains how a specific API works and what it is able to do. Its purpose is twofold. Firstly, it is an accurate reference source that describes the API in detail. Secondly, it can act as a guide and teaching tool that helps users get started and use it.  Done correctly, API documentation acts as the one true source of information for how an API works. It should contain details on functions, arguments, classes, and more in a structured format that is easy for both developers and non-technical users to understand. Often, it will include tutorials and examples, which will help the user better understand how the different parts work together. Which Specification is Best? There is more than one way to write API documentation, and different software uses different specifications. These specifications each provide a different standard and style in which an API is described. Three of the m...

Logging Request and Response Body In Spring Boot

Image
  I recently read a Gain Java Knowledge article about logging Spring Boot Request and Response. I really like the idea so I decided to test it in practice. How to logging Spring Boot Request and Response? Request and response body for each endpoint we can print using Servlet Filter. Inside our controller class we will not log any statement but our filter class will log the request and response body for each API call. So this approach will reduce the lines of code and we don’t need to worry about to add log statements in each API to print Request and response body. The Filter class will be used to log requests and responses for each API. LoggingFilter class will extends OncePerRequestFilter class because this is Filter base class that aims to guarantee a single execution per request dispatch, on any servlet container. It provides a doFilterInternal method with HttpServletRequest and HttpServletResponse arguments. Conclusion: In my opinion, this is the best solution to log request...

Schema Registry in Kafka

Image
Recently, I was inspired by an article of Amarpreet Singh . I decided to do a research. Why Schema Registry? Kafka, transfers data in byte format. There is no data verification that’s being done at the Kafka cluster level. In fact, Kafka doesn’t even know what kind of data it is sending or receiving. Whether it is a string or integer. Due to the decoupled nature of Kafka, producers and consumers do not communicate with each other directly, but rather information transfer happens via Kafka topic. At the same time, the consumer still needs to know the type of data the producer is sending in order to deserialize it. What if the producer starts sending bad data to Kafka or if the data type of your data gets changed? Than your consumers will start breaking. We need a way to have a common data type that must be agreed upon. That’s where Schema Registry comes into the picture. It is an application that resides outside of your Kafka cluster and handles the distribution of schemas to the produc...

Implementing Newsletter Subscription using Observer Design Pattern in Java

Image
Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically. Observer pattern falls under behavioral pattern category. Say, for example, you and several other readers subscribed to a newsletter. When there is a new newsletter available, you will receive it along with other subscribers. Suppose, if you don’t want to receive the newsletter, you can cancel your subscription and you will not receive the new editions. An observer design pattern is not limited to the newsletter subscription but it is a good life example.