Category: Java

0

Using lazy-loading in JPA to speed up your application

Hibernate makes working with the database a lot easier. You just need to define your entities and the library does the job of retrieving data or persisting your objects. This is true for all modern JPA libraries, like Hibernate or Spring Data. The more complex you have your objects, the more the benefits of using such libraries. However, there is a big downside to this approach and one that can make your application feel slow or sluggish. An example of...

0

An introduction to using generics in Java

I recently worked on a few benchmarks for a database and while coding it used the power of generics to write as little code as possible. In the benchmark, I had two different DAO classes and corresponding Domain Objects that were identical, with the exception of the ID field. I could write everything twice, but there is no fun in that. Instead, I wrote generic classes that accepted the ID as a type parameter. Generics is one of the most...

0

Crawling and Parsing a Website in Java using JSoup

There are many scenarios where you would need to parse the contents of a website to extract data. Search engines do this, certain applications require this functionality in order to extract information, there are integration tests that need this and even some tools must have this functionality. In this article, I will be showing how to build a website crawler in Java and how to parse the website’s content and extract information from it. Building the entire parser is quite...

0

5 simple coding tricks to make your application more stable

The stability of your application or web service is important for both you and the user. Making things resilient is not easy, but there are a few coding tricks that you can use in order to increase stability and help you solve problems easier and faster. Here are 5 simple coding tricks that will make your application more stable, will reduce errors and will make you a better coder. Comparing with a static String In many applications you will need...

0

Writing unit tests in Java using jUnit and Mockito

Testing is important for all software systems and applications. It will help you easily find bugs and it will prevent crashes and downtimes for your application, especially if the system evolves over time. At first it may not seem critical, but as the system grows and becomes more complex, the likelihood of a bug appearing without being noticed grows. There are multiple testing frameworks available for Java, with jUnit being one of the most widely used. In this article, we...

0

Cleaner Data classes with Project Lombok

One thing that I like about Java is the vast amount of libraries that are freely available. There is a library or code snippet for almost anything that you could think of or any mundane or repetitive task out there. Data classes are no exception. All applications rely on data and the presence of data classes is widespread, however, they are usually quite simple, but can take up a lot of time, especially if you have many fields. Luckily, there...

0

How to invalidate a JWT

In a past article, I wrote about JWTs, how to generate one and how to use them for authorization. JSON Web Tokens, however, have one major drawback. Once it is generated and submitted to the client, it can’t be easily made invalid. This is a big problem if the JWT got leaked and it did not expire (or worse, it does NOT have an expiration date). That is why it is important to make sure that your JWT can be...

0

Handling Exceptions and Errors in Play Framework

When programming, it is important to always take into consideration exceptions. No matter how well your code is, there can always be invalid data summited by the user problems in other libraries that can trigger exceptions. Also, using this mechanism is an easy way of terminating invalid flows as soon as possible. Exceptions are normal and should be treated in all services. Even in case of an exception, we must provide a response to the user so that he knows...

3

How to make a custom message converter for Log4J2

I’ve been using Log4J for many years. It is a powerful logging library that is efficient and highly customizable. You can extend the functionality with ease and do custom actions on the message prior to it being logged. Without too much chit-chat, in this article I will be showing you how to implement a custom converter for Log4J2. What is a converter? Log4J2 has multiple components that are called when a message is logged. You have the actual logger, which...

2

Building a REST API in Play Framework

When building web applications, REST has become the most widely used approach because it is easy to use and easy to implement. In this tutorial I will be showing how to build a simple REST API in Play Framework and will cover the 4 basic functionalities: Create – POST Update – PUT Retrieve – GET Remove – DELETE We will be creating a simple API for a student’s management software where we can manipulate the students and retrieve the stored...