Month: July 2020
-
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…