Month: June 2020
-
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…
-
How to delete unwanted files in a folder recursively using Java’s FileVisitor?
Java 7 came up with a new package for dealing with file systems : java.nio.file. It provides a better interface compared to the previous versions to deal with files and directories. One such feature is to traverse all the sub folders and files inside a folder recursively and perform any file operations on them. To…
-
How to run environment specific code in Spring Boot?
Let’s say you want to run environment specific code in your Spring Boot application. Say if the environment is local , you get data from a temporary cache and if the environment is anything higher you get it from a third party service. How to do this? You can specify the environment to be activated…
-
How and why to use Collectors.teeing() method in Java?
Java has been striving hard to reduce the verbosity of its code in the latest features. Once such feature introduced is the teeing() method introduced in Java 12. A tee means this : Input from two different sources can be combined and fed into another source. Let’s say you have a list of employees. You…