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. 

Tuesday, March 21, 2023

Difference between @RestController and @Controller Annotation in Spring MVC and REST

The @RestController annotation in Spring MVC is nothing but a combination of @Controller and @ResponseBody annotation. It was added into Spring 4.0 to make the development of RESTful Web Services in Spring framework easier. If you are familiar with the REST web services you know that the fundamental difference between a web application and a REST API is that the response from a web application is generally view (HTML + CSS + JavaScript)  because they are intended for human viewers while REST API just returns data in form of JSON or XML because most of the REST clients are programs. This difference is also obvious in the @Controller and @RestController annotation.

Monday, March 20, 2023

Spring Boot + Hibernate Tutorial and Example for Java Developers

Hello guys, if you want to learn how to use Spring Boot and Hibernate together in a Java application and look for examples and tutorials then you have come to the right place. In the past, I have shared how to use Spring Boot with MyBatis and Spring Boot + Redis example and In this article on Spring Boot, we will discuss how to integrate Spring Boot with Hibernate. We will build a simple Spring Boot application and use Hibernate to store the data. So first, let's look at what is Hibernate in Java? Hibernate framework provides an abstraction layer which means programmers need not worry about implementation. Hibernate will implement different modules for developers internally like writing queries to perform CRUD operations on the database and establishing connections to different kinds of databases. This is a Java-based solution for object-relational mapping.

Saturday, March 18, 2023

Difference between @ContextConfiguration and @SpringApplicationConfiguration in Spring Boot Testing?

Hello folks, what is the difference between @ContextConfiguration and @SpringApplicationConfiguration is one of the frequently asked Spring Boot Testing interview questions which is often used to check if the candidate knows about how to unit test Spring boot application. Even though both @ContextConfiguration and @SpringApplicationConfiguration annotations are used along with SpringJUnit4ClassRunner to specify how to load the Spring application context, there is a subtle difference between them. Although @ContextConfiguration does a great job in loading application context it doesn't take full advantage of Spring Boot features. Spring Boot applications are ultimately loaded by either SpringApplication ( in the case of the JAR) or SpringBootServletInitializer. 

What is @SpringBootApplication Annotation in Java and Spring Boot? Example Tutorial

Hello guys, if you are wondering what is @SpringBootApplicaiton annotation and how to use it then you have come to the right place. Today, we'll learn about the @SpringBootApplication annotation, one of the most important annotations from the popular Spring Boot framework, which has changed the way Java developers use the Spring framework for writing Java applications. In this article, I'll explain to you the meaning of @SpringBootApplication and its use in a simple Spring Boot application. We use @SpringBootApplication annotation on our Application or Main class to enable a host of features e.g. Java-based Spring configuration, component scanning, and in particular for enabling Spring Boot's auto-configuration feature.

Difference between BeanFactory and ApplicationContext in Spring Framework

The difference between BeanFactory and ApplicationContext in Spring framework is another frequently asked Spring interview question mostly asked Java programmers with 2 to 4 years experience in Java and Spring. Both BeanFactory and ApplicationContext provide a way to get a bean from the Spring IOC container by calling getBean("bean name"), but there is some difference in their working and features provided by them. One difference between the bean factory and application context is that the former only instantiates bean when you call getBean() method while ApplicationContext instantiates Singleton bean when the container is started,  It doesn't wait for the getBean to be called. 

Thursday, March 16, 2023

What is a Spring Data Repository? JpaRepository, CrudRepository, and PagingAndSortingRepository Example

Hello Java developers, if you are wondering what is Spring Data Repository interfaces like JpaRepository, CrudRepository, and PagingAndSortingRepository and how to use them then you have come to the right place. Earlier, I have shared the best Spring Data JPA courses and in this article, I will give you an overview of different Spring Data Repository interfaces and how to use them with step-by-step examples. The Java Persistence API (JPA) is the standard way of persisting java objects into relational databases. 

Wednesday, March 15, 2023

Why use @Override annotation in Java? Coding Best Practice Example

@Override annotation was added in JDK 1.5 and it is used to instruct the compiler that method annotated with @Override is an overridden method from super class or interface. Though it may look trivial @Override is particularly useful while overriding methods that accept Object as a parameter just like equals, compareTo or compare() method of Comparator interface. @Override is one of the three built-in annotations provided by Java 1.5, the other two are @SuppressWarnings and @Deprecated.