5 ways to convert JSON to Java Object - Using Jackson, Gson, Genson, Moshi, & FastJson

Hello everyone, today we will show you how to convert JSON to Java Object using five different Java libraries which are listed below. 1. Jackson 2. Gson 3. Moshi 4. Genson 5 FastJson In this example, we are going to convert the below JSON string to Java Object, Sample JSON String public class Json { public static String json = "" + "{\"isbn\": \"123-456-222\", \n" + " \"author\": \n" + " {\n" + " \"lastname\": \"Doe\",\n" + " \"firstname\": \"Jane\"\n" + " },\n" + "\"editor\": \n" + " {\n" + " \"lastname\": \"Smith\",\n" + " \"firstname\": \"Jane\"\n" + " },\n" + &quo

Java 8 Stream - Remove duplicates from a list of objects based on a property

Hello everyone, here we will show you how to remove duplicates from a list of objects based on a single property.  In this example, we are getting the stream from the list and putting it in the TreeSet from which we provide a custom comparator that compares id, email, or salary uniquely. Here we are going to remove duplicates based on the, id(Long) property of the user email(String) property of the user salary(Double) property of the user public class User { private Long id; private String name; private String email; private String phone; private Double salary; public Long getId() { return id; } public void setId( Long id) { this .id = id; } public String getName() { return name; } public void setName( String name) { this .name = name; } public String getEmail() { return email; } public void setEmail( String email) { this .email = email

Spring boot application.json properties example

In this article, we will show you how to use JSON instead of the properties file or the yml file in Spring Boot by custom configuration. Before moving to the example first you must be familiar with the following components. 1.spring.factories file The file is located in src/java/resources/META-INF/spring.factories . The spring.factories file is a file that spring automatically loads when booting up. It contains references to many configuration classes. For example, org.springframework.boot.env. PropertySourceLoader =com.example.demo. JsonPropertySourceLoader Here we registered our custom Json property source loader 2. YamlPropertySourceLoader Load the resource into one or more property sources. Implementations may either return a list containing a single source or in the case of a multi-document format such as YAML a source for each document in the resource. Refer import org.springframework.boot.env. YamlPropertySourceLoader ; public class JsonPropertySourceLoader ex

Using Spring Interceptor or Using Servlet Filter How To Get a Request URL Template That Has Been Hit?

In this tutorial, we will learn how to find a request URL template that has been hit using Servlet Filter or Using Spring Interceptor.  Before going to the example program just check the basis of the Servlet filter and Spring Interceptor. The Servlet Filter is used to intercept the client request and do some pre-processing before they reach the DispatcherServlet. It can also intercept the response and do post-processing before sending to the client in web application. On the other hand, The Spring Interceptor is only applied to requests that are sent to a Controller. Get request URL template using Spring Interceptor(Good approach) Demo Controller @ RequestMapping (path = "/user" ) @ RestController public class UserController { @ GetMapping (path = "/{id}" ) public ResponseEntity < Long > getAllVariables(@ PathVariable ( "id" ) Long id) { return new ResponseEntity < Long >(id, HttpStatus.OK); } } Custom

How to iterate over Array in Java?Six (6) ways to loop over an Array in Java

Iteration is a technique used to sequence through a block of code perpetually until a concrete condition either subsist or no longer subsists. Iterations are a very prevalent approach utilized with loops. In this article, we will show you  Six (6) ways to loop over an Array in Java. 1.  Using  Simple For loop  2.  Using  Enhanced For loop  3.  Using  While loop 4.  Using  Do - while loop 5. Using Stream.of() and forEach 6. Using and forEach Example 1: Using Simple For loop     // create an array String [] users = { "alpha" , "beta" , "giga" , "gama" , "tesla" }; // Simple For loop for ( int i = 0 ; i < users.length; i++) { System.out.println(users[i]); } Example 2:  Using Enhanced For loop  The enhanced for loop is introduced since J2SE 5.0. It is used to traverse the array or collection elements.  // create an array String [] users = { "alpha