Spring MVC Interview Questions for Freshers

1. What is MVC?

MVC refers to Model, View, and Controller. It is an architectural design pattern, which governs the application’s whole architecture. It is a kind of design pattern used for solving larger architectural problems.

MVC divides a software application into three parts that are:

  • Model
  • View
  • Controller

2. What is Spring MVC?

Spring MVC is a sub-framework of Spring framework which is used to build dynamic web applications and to perform Rapid Application Development (RAD).

  • It is built on the top of the Java Servlet API.
  • It follows the Model-View-Controller Architectural design pattern.
  • It implements all the basic features of the applicationscore Spring framework like IOC (Inversion of Control) and Dependency Injection (DI) etc.

To learn more about the Spring MVC Framework, refer to this article

3. Difference between Spring Boot and Spring MVC

Features
 

Spring Boot

Spring MVC

Build It is a framework, that helps developers get started with Spring framework with minimal configuration. It is a web framework built on the top of Java Servlet API.
Working Using Spring Boot, it is easy to create stand-alone dynamic web applications and rapid application development. It is a part of core Spring framework, which supports Spring’s basic features and is used for building web applications using MVC architecture.
Productivity Developers use Spring Boot to save time and increase productivity in developing stand-alone applications and Spring-based projects. Developers use Spring MVC to create web applications running on a servlet container such as Tomcat.

4. Explain Spring MVC Architecture.

Spring MVC Architectural Flow Diagram:

  • First, the request will come in through the browser and it will be received by Dispatcher Servlet, which will act as Front Controller.
  • Dispatcher Servlet will take the help of handler mapping and get to know the controller class name associated with the request.
  • After this, it will transfer the request to the controller, and then the controller will process the request by executing appropriate methods based on used GET or POST method.
  • And it will return the ModelAndView object back to the dispatcher servlet.
  • Now, the dispatcher servlet sends the model object to the view resolver in xml file to get the view page.
  • And finally, the dispatcher servlet will pass the model object to the view page to display the result.

5. What are the Key Components of Spring MVC Architecture?

Below are the Key Components of Spring MVC Architecture:

  • Dispatcher Servlet
  • Handler Mapping
  • Controller
  • Model
  • View
  • ViewResolver
  • HandlerInterceptor
  • LocaleResolver
  • MultipartResolver
  • WebDataBinder
  • ModelAndView
  • HandlerExceptionResolver

6. Explain the Model-View-Controller (MVC) Design Pattern.

MVC design pattern is a way to organize the code in our application. MVC refers to Model, View, and Controller.

  • Model – It represents data, which is coming in our website URL as a query parameter.
  • View – It represents the model data in a structured format (view page) which the end-users are going to see.
  • Controller – It represents the business logic of an application, resides inside the controller and to mark a class as a controller class we use @Controller annotation.

Below is the Model-View-Controller Flow diagram:

Know more about MVC design pattern

7. What is Dispatcher Servlet in Spring MVC?

Dispatcher Servlet is the Front Controller in the Spring MVC framework. This is a class that receives the incoming HTTP requests and maps these requests to the appropriate resources such as model, controller, and view. Also, it sends the appropriate responses to the requests. Dispatcher Servlet manages the entire flow of an application.

Know more about dispatcher servlet

8. Explain the five most used annotations in Spring MVC Project.

The most used five annotations in the Spring MVC project are:

  • @Controller: This annotation is used to create classes as controller classes and parallelly it handles the HTTP requests as well.
          @Controller
public class GfgController {
// write code here
}

  • @RequestMapping: To map the incoming HTTP requests with the handler methods inside the controller class, we use @RequestMapping annotation.
           @RestController
public class GfgController {
@RequestMapping(value = "", method = RequestMapping.GET)
//write code here
}

  • @RequestParam: To obtain a parameter from URI (Uniform Resource Identifier), we use @RequestParam annotation.
         @GetMapping("/clients)
public String getClients(@RequestParam(name = "clientname") String name) {
//write code here
}

  • @PathVariable: To extract the data from the URI path, we use @PathVariable annotation.
          @GetMapping("/client/{clientName}")
public String getClientName(@PathVariable(name = "clientName") String name) {
//write code here
}

  • @ModelAttribute: This annotation binds method parameter and refers to the model object.
          @ModelAttribute("client")
public Client client() {
//write code here
}

9. What is ViewResolver in Spring MVC?

In Spring MVC, ViewResolver is used to determine how a logical view name is received from a controller and maps that to an actual file. There are different types of ViewResolver classes. Some of them are defined below:

  • InternalResourceViewResolver: It uses a prefix and suffix to convert a logical view name.
  • ResourceBundleViewResolver: It uses view beans inside property files to resolve view names.
  • XMLViewResolver: It also resolves view names in XML files to beans defined in the configuration file.

Know more about ViewResolver in Spring MVC

10. Difference between @Controller and @RestController

Features
 

@Controller

@RestController

Usage It marks a class as a controller class. It combines two annotations i.e. @Controller and @ResponseBody.
Application Used for Web applications. Used for RESTful APIs.
Request handling and Mapping Used with @RequestMapping annotation to map HTTP requests with methods. Used to handle requests like GET, PUT, POST, and DELETE.
  • @RestController annotation encapsulates @Controller and @ResponseBody annotation.

@RestController = @Controller + @ResponseBody

11. What is WebApplicationContext in Spring MVC?

WebApplicationContext is an extension of ApplicationContext. It has servlet context information. We can use multiple WebApplicationContext in a single web application which means every dispatcher servlet is associated with a single WebApplicationContext.

A web application can have more than one dispatcher servlet to handle HTTP requests and every front controller has a separate WebApplicationContext configuration file. It is configured using *-servlet.xml file.

12. What is DTO and Repository Interface in Spring MVC?

DTO: DTO stands for Data Transfer Object. It is a simple model class that encapsulates other different objects into one. Sending the data between client and server requires a model class. When the client requests data from the server, instead of sending multiple responses it will send only one.

Note: DTO should not contain any additional logic, except the logic for encapsulation.

       data class GfgAuthor (
val name: String,
val age: Int
)

Repository Interface: To establish database connectivity and data access, we define logic and methods inside the repository class, and we define this interface by putting @Repository annotation.

Note: Repository implements any one of the pre-defined repositories like CRUD repository or JPA repository.

Watch this video on DTO in Spring MVC

13. How to handle different types of incoming HTTP request methods in Spring MVC?

To handle different types of HTTP request methods we use @RequestMapping annotation in Spring MVC. For mapping incoming HTTP requests with the handler method at the method level or class level, @RequestMapping annotation is being used.

@RequestMapping(value = "", method=RequestMapping.GET)

There are different types of methods for HTTP requests.

  • GET
  • POST
  • PUT
  • DELETE

For each request,run we can use separate annotations like @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping instead of passing the method inside the @RequestMapping annotation.

Ex- @GetMapping("/hello")

14. Difference between ApplicationContext and WebApplicationContext in Spring MVC

Features
 

ApplicationContext

WebApplicationContext

Working It is designed for stand-alone applications. It is designed for web applications that run within a container (web container) like Tomcat or  Jetty.
Configuration It is configured using applicationContext.xml or @Configuration and @Bean annotation. It configured using XML file *-servlet.xml
Example Desktop Applications RESTful APIs

Spring MVC Interview Questions and Answers

Spring MVC (short for Model-View-Controller), is a Java framework designed to simplify web application development. Spring MVC is part of the larger Spring Framework, which was created by Rod Johnson and released in 2002.

In this article, we provide you with the Top 35+ Spring MVC interview questions with answers that cover everything from the basics of Spring MVC to advanced Spring MVC Concepts Such as Dispatcher Servlet, Interceptors, Data Binding and Validation, RESTful APIs, and many more.

Whether you are a fresher or intermediate (minimum 1 year of experience ) or an experienced professional with 5 years or 10 years of experience, these practice spring MVC interview questions give you all the confidence you need to ace your next Spring MVC Technical interview.

Table of Content

  • Introduction to Spring MVC
  • Spring MVC Interview Questions for Freshers
  • Spring MVC Interview Questions for Intermediate(1+ years)
  • Spring MVC Interview Questions For Experienced(5+years)
  • Bonus Spring MVC Questions and Answers
  • Advantages of Spring MVC
  • Future Trends and Updates in Spring MVC

Similar Reads

Introduction to Spring MVC

Spring MVC is a reliable and widely used framework for building web applications in Java. It is a part of the larger Spring Framework and follows the Model-View-Controller (MVC) design pattern. It allows developers to create scalable, flexible, and efficient web applications....

Spring MVC Interview Questions for Freshers

1. What is MVC?...

Spring MVC Interview Questions for Intermediate(1+ years)

15. How to perform Validation in Spring MVC?...

Spring MVC Interview Questions For Experienced(5+years)

22. Explain Spring MVC Interceptor....

Bonus Spring MVC Questions and Answers

1. What is Additional configuration file in Spring MVC?...

Advantages of Spring MVC

Spring MVC is beneficial in many aspects, it provides value to developers as well as applications. Some advantages of Spring MVC are:...

Future Trends and Updates in Spring MVC

Spring MVC is considered the best framework in the JAVA ecosystem because it follows future requirements and slowly adds new features to the framework. Some future updates anticipated in Spring MVC are:...

Conclusion

In this article, we have covered Spring MVC interview questions for beginners, intermediate and advanced professionals. Whether you are looking to start a new career or want to switch your job, we have got you covered....

Frequently Asked Questions – Spring MVC Interview Questions

Q. How to prepare for the Spring MVC interview?...