Author: Vijay SRJ

  • How and when to use computeIfPresent() and computeIfAbsent() methods of Map interface in Java?

    Let’s assume you are developing a java app to keep score of goal scores of football players. Every time a player scores a goal , you call a method with the name of the player and the score gets incremented for that player. What data structure would you choose for this? Ideally a map with…

  • How to log IP addresses of incoming requests to a Spring Boot app in a centralized way?

    Let’s say you want to track the source of hits coming to your REST services. And you have developed them using Spring Boot. Here is the algorithm: STEP1: Create a Spring Handler Interceptor STEP2: Retrieve the IP address in the prehandle method of the Handler interceptor. 2.a) Use X-Forwarded-For header extracted from HttpRequest object to…

  • How to send a custom error message for bad requests (400 error code) in Spring Boot?

    Let’s say you created a REST API which accepts a JSON input. You handle any type of exception in your code and throw a custom error message to the user. But what if the user sends a bad JSON ? Spring framework will throw a predefined error message. Below is the error message I got…

  • Day 100 – Wrapping It Up

    Feeling a sense of relief, today is the 100th day of another 100 day project : Post one technical post per day for 100 days. On the third day after lockdown was announced in Tamilnadu, I started working on this idea. This has kept my mind occupied. I have stopped following news about corona spread…

  • Introducing JCli – A command line tool for creating Java applications

    Imagine you could create your java applications from command line using a tool. Just like you would use ng tool for Angular and npm for node js. This would save a lot of developer time. With this in mind I experimented with creating a command line tool called “jcli” (java client) using Java and picocli…

  • How to use Optional keyword to avoid NullPointerException in nested objects in Java?

    Let’s say you have a nested object in your application . And you want to retrieve the value of a field in the deepest nested object. We will see how to do this using pre-Java 8 and post-Java 8 code (using Optional keyword) Lets consider an Employee object, Let’s say the Employee object has an…

  • How to create Immutable collections in Java?

    Let’s say you want to create a list of objects which cannot be modified. There are three ways to do this in Java: Using Collection Factories Using methods in Collections utility class Using methods in Collectors utility class Using Collection Factories: Java 9 came up with collection factories to create immutable collections. To create an…

  • How to use services in Java Modules?

    Java 9 came up with the module system. This post gives a minimalist explanation of how to create modules in java. Let’s say you have decided to use modules in your application. And you are going to buy and sell coconuts using these modules as done in the post highlighted. And so you have created…

  • How to create Java Modules?

    Java 9 introduced the Module system under the project name Project Jigsaw. This altered the existing structure of Java projects. Java projects can be now encapsulated into modules and each module can be separately packaged into a jar or a run time (customized JRE). Until the module system came out , every Java project has…

  • On Maven Wrapper

    Let’s say you have built a project using maven. You used a specific version of maven to build it. Now you want to send this project to your friend. To build your project , your friend first needs to install maven in his machine. And also he might need to use the specific version you…