Monday, April 3, 2023

Java 8 - Consumer Functional Interface Example

Hello guys, if you are wondering what is a Consumer functional interface in Java and when and how to use it then you have come come to the right place.  In the last few article, I have explained what is Functional interface and showed example of Predicate Interface and in this article, I will show you how to use Consumer functional Interface in Java.  A Java Consumer is a predefined function interface that has been introduced by java 8. This type of interface accepts only one argument in order to generate the result. 

How to use Stream range, rangeClosed, sum and sorted methods? Java 8 IntStream Examples -

Hello guys, if you want to learn about IntStream in Java 8 and looking for a simple tutorial and example then you have come to the right place. Earlier, I have shared 10 examples of Lambda expression and filter+ map+ collect examples and in this article, I will show you how to use IntStream in Java 8. IntStream is a specialization of Stream class for primitive int values. It allows you to perform a sequential and parallel aggregate operation on a list of values like count() to print the total number of element in stream, min() to find minimum value in the stream, max() to find maximum value in the stream, sum() to find a total of all values and average() to find the average of all numbers in the stream.

What is Spring MVC Controller? What is the role of @Controller in Spring MVC?

Spring MVC is one of the popular Java framework to develop web application. It is based upon Model View Controller design pattern, that's why Spring MVC. The Core Spring framework jsut provide dependency injection and container but its the Spring MVC which is responsible for routing requests, handling them and resolving to views. In last couple of article, I have taught you how Spring MVC works and the role of DispatcherServlet in Spring MVC framework and today, I'll explain about Spring MVC Controllers. Many Java developer think that DispatcherServlet is the controller in Spring MVC which is partially true. DispatcherServlet is Front Controller which means all request will go through it but it doesn't handle any request by itself, instead it delegates to Spring MVC controllers for further request processing. 

Sunday, April 2, 2023

How to use map + flatMap + Collectors.toSet() in Java? Example Tutorial

Hello guys, I was searching for a good example of flatMap online but didn't found a good one, so created my own. As name suggest flatMap is used to flatten stream like unwinding multiple list of values. For example you have a Stream of List of Integer and you want to create Stream of Integer by unwinding those list, you can do that using FlatMap function from java.util.Stream class. Coming back to flatMap, its actually a general concept which is available in other functional programming language like Scala. Just like map() function works by applying a function to each element in the Stream, flatMap works by applying a function that returns a sequence for each element in the list, and flattening the results into Stream or another list. 

7 Examples of Comparator and Comparable in Java 8

Hello guys, you may know that Java 8 introduced a number of features to simplify the code and make it more readable. One such feature is enhancements of Comparator and Comparable interfaces. The Comparator interface provides a way to compare two objects and sort them based on specific criteria. The Comparable interface is used to compare the objects of the same class and sort them based on their natural order. In this article, we'll explore 7 different examples of Comparator and Comparable in Java 8. We'll see how we can use them to sort objects based on various criteria, such as alphabetical order, length, age, and salary. These examples will give you a better understanding of how Comparator and Comparable work and how you can use them to improve your code.

Thursday, March 30, 2023

Difference between First and Second Level Cache in Hibernate [Answer]

If you have used Hibernate in past then you know that one of the strongest points of the Hibernate framework is caching, which can drastically improve the performance of the Java application's persistence layer if configured and used correctly. Hibernate provides caching at many levels e.g. first level cache at Session level, second-level cache at the SessionFactory level, and query cache to cache frequently executed SQL queries. The first level cache minimizes database access for the same object. For example, if you call the get() method to access Employee object with id = 1 from one session, it will go to the database and load the object into memory, but it will also cache the object in the first-level cache.

Wednesday, March 29, 2023

Difference between get and load in Hibernate

get vs load in Hibernate
Difference between get and load method in Hibernate is one of the most popular questions asked in Hibernate and spring interviews. Hibernate Session class provides two methods to access object e.g. session.get() and session.load() both looked quite similar to each other but there are subtle differences between load and get method which can affect the performance of the application. The main difference between get() vs load method is that get() involves database hit if an object doesn't exist in Session Cache and returns a fully initialized object which may involve several database calls while load method can return proxy in place and only initialize the object or hit the database if any method other than getId() is called on persistent or entity object.

Monday, March 27, 2023

Difference between save vs persist and saveOrUpdate in Hibernate

Save vs. saveOrUpdate vs. persist in Hibernate
What is the difference between save and saveOrUpdate or Difference between saving and persist are common interview question in any Hibernate interview, much like the difference between get and load method in Hibernate. Hibernate Session class provides a couple of ways to save an object into the database by methods like save, saveOrUpdate, and persist.  You can use either save(),  saveOrUpdate() or persist() based upon your requirement for persisting objects into the database. The key thing is that all these objects are used to store data into the database but they also make a transient object persistent in Hibernate.

Saturday, March 25, 2023

How to send HTTP POST Request with JSON Payload to REST API using Curl Command?

There are many ways to test your RESTful Web services developed using Spring MVC e.g. you can use RESTAssured for automation testing or you can use Postman to manually test your RESTful Web service but nothing beats the elegant, comfort, power, and simplicity of using curl command to test RESTful web service. It is my favorite tool to test any RESTful web service and I have used in past to test various aspect of Spring REST application e.g. testing headers, testing authentication and authorization, testing different content types like JSON and XML, testing cookies, and testing different HTTP methods to perform CRUD operations. 

Friday, March 24, 2023

Difference between REST and GraphQL API with Example

Hello guys, I have been learning GraphQL since last week and should I say, I am really impressed with its flexibility and how it address some of the pertinent problems with REST APIs. If you don't know, The GraphQL is a query language from Facebook which aims to solves some common problems with REST like a explosion of endpoints, over fetching and under fetching of data, response structure, versioning, and most important performance and Scalability. If you have used REST APIs then you know that you need to send a lot of request to get the data you want. You not only need send multiple request but also you get a lot of unnecessary data which you don't really need, and also need to know multiple endpoints. 

Thursday, March 23, 2023

What is the use of DispatcherServlet in Spring MVC Framework?

If you have worked with Spring MVC then you should know what is a DispatcherServlet? It's actually the heart of Spring MVC, precisely the C of MVC design pattern or Controller. Every single web request which is supposed to be processed by Spring MVC goes through DispatcherServlet. In general, its an implementation of Front Controller Pattern which provides a single point of entry in your application. It handles all incoming requests. It is also the bridge between Java and Spring. Btw, the DispatcherServlet is like any other Servlet is declared in the web.xml with a URL pattern but the only special thing is that the URL pattern for dispatcher servlet is enough to map every single web request to DispathcherServlert.

7 Reasons to use Spring Framework for Creating RESTful Webservices in Java

REST has now become a standard way to develop web services and when it comes to Java, there are many frameworks and libraries available like JAX-RS, Restlet, Jersey, RESTEasy, Apache CFX, etc, but I encourage Java developers to use Spring framework to develop RESTful web services. But, some of you might ask, why use Spring Framework to develop RESTful web services in Java? What is the advantage and why it's better than other frameworks and libraries available out there? Well, the most important reason I think to use Spring for developing RESTful web service is that you can use your Spring MVC experience to develop RESTful web services.

Eclipse - How to Fix java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Tomcat

Problem: You are getting java.lang.ClassNotFoundException exception complaining that Spring's DispatcherServlet class is not available in the classpath. This error is coming while running a Spring MVC based Java application from Eclipse and Tomcat as Server (running inside Eclipse IDE itself). You have either included spring framework JAR files manually by yourself or you are using Maven to download and manage dependent JAR files. Here is the stack trace of this error :

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:126)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957

Wednesday, March 22, 2023

20 Tools DevOps Engineers Should Learn in 2024

Hello guys, If you are want to become a DevOps Engineer in 2024 or just want to take your DevOps skills to next level and wondering which tools to learn in 2024 then you have come to the right place. Earlier, I have shared best DevOps Courses, Books, and even a complete DevOps RoadMap and in this article, I am going to share essential tools for DevOps professionals in 2024. These tools includes build tools like Maven and Gradle, packaging tools like Ansible and Chef,  CI and CD tools like Jenkins, version control tool like Git, container tools like Docker and Kubernetes, as well as advanced monitoring tools like Grafana and Prometheus. 

How View Resolver works in Spring MVC? [InternalResourceViewResolver Example]

Hello guys, if you are wondering what is view resolver in Spring MVC and what role InternalViewResolver class plays in Spring MVC then you have come to the right place. Earlier, I have explained to you how Spring MVC works internally and how it processes HTTP requests coming to your web application. One of the important parts of that processing was resolving views, which is handled by the ViewResolver interface, which is the V part of MVC. They are responsible for returning correct views to the client, both in the Spring MVC application as well as on REST APIs. In this article, you'll learn more about view resolvers in Spring MVC by explaining the InternalResourceViewResolver class.