Author: Vijay SRJ
-
How to use the var keyword in Java?
Java is statically typed. It means the type of a variable is known at compile time. And it is strongly typed , an integer variable will always be an integer variable . Starting Java 10 , Java introduced the concept of local variable type. Since then you can use var keyword to represent variables. This…
-
Sealed classes and interfaces in Java
Let’s say you are creating a base class and you want it be extended by other developers to reuse the code. Say you developed a base class named Animal and you want to allow another developer to create a Cat class which can extend this base class. So you made the base class as public…
-
How to create a command line tool using Java?
Java applications can be run using command line , but you can pass only arguments . Based on the position of the arguments , you can retrieve them in your application and code your logic. What if you can pass flags to your java application and let it execute logic based on that. Like in…
-
How to create a Windows Native Java application (generating .exe file)?
Let’s say you have created a Java application and packaged it into a jar. You want to install this jar in another machine. Traditionally , you need to first install java in that machine and then copy the jar . The application can be launched by running the jar. Or you can use third party…
-
How to restart your Spring Boot application automatically for every code change?
One factor which slows down developer’s productivity is restarting the application for every code change to reflect. And even start up time takes time. The time is much less in Spring Boot compared to traditional MVC applications. And new frameworks like Micronaut even have faster start up times. To avoid the manual restart , Spring…
-
How to use Spring WebClient to invoke REST services reactively and non reactively?
Let’s say you want to invoke a reactive REST service developed using Spring WebFlux. A reactive REST service is one which lets you invoke itself asynchronously. You either get a Mono response (for a single object response) or a Flux response (for multiple objects). Here is a demo of how to call a reactive REST…
-
How to create a reactive REST service using Spring WebFlux?
Reactive programming is gaining more popularity now than ever before. Traditionally when you invoke a REST service it becomes a blocking call. You need to wait until you get the response and then proceed with your next line of code. With reactive programming , you can make an asynchronous call and then retrieve the response…
-
How to create a Database and perform CRUD operations in MongoDB through command prompt?
MongoDB is one of the most popular NoSQL databases . Instead of inserting rows into tables as in traditional relational databases you insert documents into collections in a MongoDB. The documents have the structure of a JSON. And the documents dont have to have a defined structure as tables need to in a relational database.…
-
How to set read time out for calling REST services using Spring REST template ?
Let’s say you are invoking a REST service using Spring’s REST template. And you want to set the read time out to a certain value. You don’t want the invoked service to take too much time to send data. You can do this in two ways: Set the time out locally Set the time out…
-
How to read and write to files easily with java.nio.file package
Prior to Java 7 reading and writing to files in Java was not intuitive. We had to depend on java.io package. To write to a file you had to use a file writer like this : You had to create a new instance of FileWriter and then also make sure it is closed once the…