How to pretty print JSON in Java

Here I am going to show you how to pretty print JSON in Java. I am going to use here JSON, Jackson and Google Gson library to prettify the JSON. I am also going to show you how to read and write JSON file using Java 8 or later version. I am going to show you how to prettify JSON string as well as JSON file data. Why do you need to prettify the JSON data? Because you want to read this JSON data manually when you received this data…

Continue

How to generate PDF report from MySQL database in Spring Boot

I am going to show yo how to download PDF file from MySQL database using Spring Boot and iText library in Java programming language. Many applications require dynamic generation of PDF documents for reporting purpose, such as, generating statements for customers, readers buying online specific chapters of a book, etc. In my another tutorial I had shown how to create or write to a PDF file using Java based iText library. The iText library is freely available and can be used using maven or gradle build tool. We can also…

Continue

How to convert PDF File to CSV File using iText API in Java

In this example I going to show you how to convert PDF file to CSV file. I will read the PDF file using iText library and write data to the CSV file using Java programming language. In my previous example I had shown how to convert CSV file to PDF file using iText library. As you know that CSV is a comma separated value, so I assume that the PDF file is having data in tabular format which would be converted into comma separated values. Related Posts: How to generate…

Continue

How to convert CSV file to PDF file using iText API in Java

Here I am going to tell you how to convert CSV to PDF file using iText library in Java programming language. I am going to use Java 7 or later version’s new feature Path API to read the CSV file and Java 8’s Stream API to split the comma separated line or record. I am going to show you how to read file content into byte array and how to convert byte array into String. Then I am converting the array of String into a list of String. Finally from…

Continue

Junit Testing for Multiple Files Upload Spring REST Controller

Introduction Here I am going to write Junit test case for testing multiple files upload in Spring REST Controller. I have already created how to upload multiple files using Spring REST API and I also had shown how to test it through REST client tool – Postman. In this tutorial I am going to use Junit 5, @WebMvcTest annotation to test the multiple files upload REST API. @WebMvcTest annotation can be used not only to test Spring Controllers but also Spring REST Controllers. I will build the applications using both maven…

Continue

Java Junit 5 Mockito doNothing() Example

In this tutorial I am going to show you how to work with Mockito‘s doNothing() in Java Junit 5 API. Mockito‘s doNothing() or Mockito itself is widely used testing framework. If you are working with Junit 4 then you can read the similar example Mockito’s doNothing() using Junit 4. Mockito‘s doNothing() is used when you want to test void methods because void methods do not return anything so there is no way you can verify using assert. These void methods may be anywhere, for example, in service class, in dao…

Continue

How to select single or multiple records using JDBC executeQuery() and executeQueryForList()

Here we will discuss how to select single or multiple records using JDBC API. To select single record from database we use executeQuery() and to select multiple records we use executeQueryForList(). You may need to use executeQuery() to fetch single record from the database for various reasons, such as: You want to see details for a particular record You want to update a particular record You want to check whether the record exists or not

Continue

Batch insert and batch update into DataBase using JDBC PreparedStatement

Introduction Here I am going to tell you how to insert and update a list of objects in batch using JDBC PreparedStatement in Java programming language. Generally when you have a list of objects or lots of data then probably it is not a good idea to insert or update individual record or object into database, because it makes lots of database calls. So to avoid such too many database calls we insert or update the records into batch and commit the transaction at the end of the batch execution.

Continue

How to add comments in excel sheet cell using Apache POI Java API

Introduction In excel file we have seen there are comments on text cells or columns. So in this tutorial we will see how to add or insert comments in excel sheet cell using Apache POI Java API. Comments can help you to remember what all are formulae or instructions you want to provide to other excel users. In this example we will create an excel file using Apache POI and Java programming language. In the excel file we will create a sheet and on particular columns on particular rows we…

Continue

Should we synchronize run() method?

Here in this tutorial we will discuss whether we should synchronize the run() method or not. Another question may arise can we synchronize run() method? In simple word, yes, we can synchronize the run() method. Do we need to synchronize the run method? When it comes to whether we need to synchronize or not, then it is not necessary to synchronize the run() method as this method gets executed by a single thread only.

Continue