Posts

Showing posts from April, 2022

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.

Debug Java Stream in Intellij Idea

Image
Stream is a very powerful feature. it allows you to take full advantage of modern multi-core architectures and lets you process data in a declarative way.  Unfortunately stream API may sometimes be difficult to debug. This happens because they require you to insert additional breakpoints and thoroughly analyze each transformation inside the stream.  IntelliJ IDEA provides a solution to this by letting you visualize what is going on in Java Stream operations. Just install plugin called “Java Stream Debugger”. Once you have enabled this plugin you can simply debug your code. This plugin will bring a trace icon (Trace Current Stream Chain button).   Trace Current Stream Chain button Once you click on that icon you get the visualization of your stream pipeline.  For every stream operation, we have got a dedicated tab. you have to switch to the relevant tab to understand what it is doing.