Category: Java

1

Getting NullPointerException when using an Enum? Here is how to fix it!

I started working on a technical debt story at my job. I am working on the back-end for a banking application for one of the biggest banks in Romania. This story involved doing some refactoring that will make the application more maintainable and easier to understands. The refactoring, however, managed to baffle me for almost three hours and only after involving the team we managed to solve an error I did not even knew it was possible: NullPointerException when using...

0

How to log HTTP Request and Response in Spring Boot

When you are working on complex systems that are built using multiple micro-services, you will need to know the integrations between them, especially when you are accepting user input. I’ve worked most of my career in the banking and payments industry and knowing what was received and what was returned back to the user is not only important for investigating problems, but a requirement for auditing in certain scenarios. And even if you are not legally required, it is extremally...

0

Masking log output in Logback

It is important to make sure that sensitive information does not get logged by your application. Logging is an important tool for troubleshooting your app, however, it can be easy to accidentally log passwords, emails, or other sensitive information. For such scenarios, I created Logmasker, an open-source library that can be used with both Logback and Log4j2 for masking all sorts of sensitive information. Please look at the dedicated page for LogMasker – Log4j and Logback Masking Library. However, because of...

better_scheduled_jobs_in_spring 0

How to write more stable Scheduled Cron Jobs in Spring

Up until now, I wrote most of my articles for Play Framework, even though I use almost exclusively Spring Boot in my daily job. Well, how about we change things a bit and this time I will be writing about Spring. And what better article than a way to make your code more stable and easier to maintain and debug? So, Spring has a feature that lets you schedule job executions at certain times or at regular intervals. The @Scheduled...

0

Better unit test with JUnit 5 Parameterized Tests

In many situations, you have to write unit tests that are identical, with the exception of the input or possibly the expected value. It is tempting to write the same test multiple times or to write one test with the dynamic part in a loop. JUnit 5, however, offers a better option with the @ParametrizedTest annotation that allows you to have one test, but with different inputs and expected values. Let’s take a look. Importing the library into your project...

0

How to lock entries in the database to avoid double-processing using Update with limit

Many services have some sort of job that gets executed at regular intervals and does some sort of processing or aggregation. In many instances you read data from the database, do the needed calculations and write the result. This is pretty straightforward and normally does not pose any major problems. You have @Scheduled in Spring, Scheduled Tasks in Play or a simple CRON job that gets executed at regular intervals. Everything works fine if you have one instance that does...

2

Masking log output in Log4j2

Users are demanding more and more privacy and as a web developer, you have to make sure that sensitive information does not get leaked as best as you can. Most of you have good security measures set in place that protect from most common attacks, however, one part of the application that is usually neglected are the logs. This is because logs are usually meant to remain internal. This is not always the case and even if it was, if...

2

An argument for using Optional as input parameters

This article may be the hardest I wrote so far here. Not because it is really technical nor because it required a lot of research prior, but because it is controversial and I had to be sure that the arguments I will present here are good. Still, I know that some will not agree with me. So let’s get down to business. There is an ongoing debate in the Java community regarding Optional and if it should be used as...

0

Intercepting and manipulating requests in Play Framework

There are situations when you have to intercept all requests coming into your application in order to do some processing prior to handling the request or doing some cleanup after it was handled. A good example that I usually give, and one that is widely used in many applications, is fishtagging for logging. This is when you write a unique identifier in the thread context and all messages are logged under the same tag (or NDC, as it was formally...