Spring MVC Interview Questions for Intermediate(1+ years)

15. How to perform Validation in Spring MVC?

There are so many different ways to perform validation in Spring MVC.

  • First Approach: Annotation based Validation – Using @Valid, @NotNull, and @Email annotations which are based on JSR-303 Bean Validation to define validation rules on a model attribute.
          public class GfgAuthor {
@NotNull
private String name;
@Email
private String emailId;
}

  • Second Approach: By implementing org.springframework.validation.Validator interface or we can say this custom validator interface.
  • Third Approach: Validate manually is the common approach to perform specific validation.

Know more about Spring MVC Validation

16. How to perform Exception Handling in Spring MVC?

Exception means an unexpected error that occurs during application execution. To identify or handle that error, called Exception Handling.

In Spring MVC, we can handle exceptions using many mechanisms, three of which are described below:

  • @ExceptionHandler annotation: It allows us to define different methods to handle different exceptions in the application.
        @ExceptionHandler(ResourceNotFound.class)

  • HandlerExceptionResolver Interface: To handle exceptions, this interface allows to implement custom logics and allow to create own exception resolver that can handle different types of exceptions thrown by the application.
        public class GfgExceptionHandler implements HandlerExceptionResolver

  • Log Exception: This exception-handling mechanism is used for debugging and analysis of an application.

17. Difference between @RequestParam and @PathVariable annotations in Spring MVC

Features
 

@RequestParam

@PathVariable

Binding Value From the URL query parameter, it binds the value. From dynamic segments in the URL path, it binds the values.
Requirement This annotation is always Optional by default. This annotation is always Required by default.
Syntactical Example @RequestParam(name=”author”) String author @PathVariable(“author”) String author

18. Explain Query String and Query Parameter in Spring MVC.

In Spring MVC, Query String and Query Parameter are used to pass the data to a web application through URL.

  • Query String: In a URL, the query string comes after “?”. It contains key-value pairs that are separated by “&”.
        https://gfg.org?path?key=value&key1=value1

  • Query Parameter: In a query string, the key-value pair is called the query parameter.
    • Key: name of data
    • Value: actual data

To access query parameters @RequestParam and @PathVariable annotations are used in a Spring MVC application.

19. Define the purpose of the @ModelAttribute annotation.

@ModelAttribute in Spring MVC has many purposes but the two main purposes are defined below:

  • This annotation enables binding the method parameter value and returns the value of a method to a named attribute.
  • This annotation allows us to populate the model object data and that data can be accessed by viewing pages like Thymeleaf, Freemarker, etc. and then we can also view the data.

Know more about @ModelAttribute annotation

20. Difference between @RequestBody and @ResponseBody Annotation in Spring MVC

Features
 

@RequestBody

@ResponseBody

Usage This annotation is used to convert incoming HTTP requests from JSON format to domain objects. This annotation is used to convert domain objects to JSON format.
Binding Here method parameter binds the request body. The Return type of method binds with the response body.
Data Transfer Receives data from the end user. Sends data to end user.

21. Explain the Multi Action Controller in Spring MVC.

Multi Action Controller in Spring MVC is a unique controller class (MultiActionController) that is used to handle multiple HTTP request types like GET, PUT, POST ETC. There are many advantages of Multi Action Controller.

  • It reduces code duplication, simplifies maintenance, and increases flexibility.
  • It manages and implements CRUD operations.

Note: Multi Action Controller is not a best option for complex logics.

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?...