Top 50 Spring MVC Interview Questions


  1. What is Spring MVC?
    • Spring MVC is a framework built on top of the Spring framework that provides a model-view-controller architecture for developing web applications in Java.
  2. Explain the architecture of Spring MVC.
    • The architecture of Spring MVC consists of a front controller called DispatcherServlet, which receives incoming requests and dispatches them to the appropriate controller. The controller processes the request, interacts with the model (business logic), and then returns the appropriate view to be rendered to the user.
  3. What is the role of DispatcherServlet in Spring MVC?
    • DispatcherServlet is the front controller in Spring MVC. It receives all incoming requests and then dispatches them to the appropriate controller for processing. It also manages the overall request lifecycle, including invoking interceptors, resolving views, and handling exceptions.
  4. How does Spring MVC handle HTTP requests?
    • Spring MVC handles HTTP requests by mapping them to controller methods based on the URL patterns defined in the request mapping annotations or XML configuration. Once a request is mapped to a controller method, the method is invoked to process the request and generate the response.
  5. What are the key components of Spring MVC?
    • The key components of Spring MVC are DispatcherServlet, Controller, Model, and ViewResolver.
  6. Explain the role of Controller in Spring MVC.
    • The Controller in Spring MVC is responsible for processing incoming requests, interacting with the model to perform business logic, and returning the appropriate view to be rendered to the user.
  7. What is a ViewResolver in Spring MVC?
    • A ViewResolver in Spring MVC is responsible for resolving logical view names returned by controllers to actual view implementations (such as JSP, Thymeleaf, or FreeMarker templates).
  8. How does Spring MVC handle form submission?
    • Spring MVC handles form submission through data binding, where form fields are automatically mapped to the properties of a JavaBean object. This can be achieved using the @ModelAttribute annotation or by directly binding form fields to method parameters in controller methods.
  9. What is the purpose of ModelAndView in Spring MVC?
    • ModelAndView is a class in Spring MVC that represents both a model and a view. It allows controllers to pass data to views and specify which view should be rendered as a response to the user’s request.
  10. Differentiate between @Controller and @RestController annotations.
    • @Controller is used to define a class as a controller in Spring MVC, whereas @RestController is a specialized version of @Controller used for RESTful web services. @RestController automatically converts the response to JSON or XML.

  1. How can you handle exceptions in Spring MVC?
  • Exceptions in Spring MVC can be handled using the @ExceptionHandler annotation at the controller level or by implementing a HandlerExceptionResolver to handle exceptions globally across the application.
  1. Explain the concept of Model in Spring MVC.
  • The Model in Spring MVC represents the data that the application works with. It can be a POJO (Plain Old Java Object) or a Map, and it holds the data that needs to be passed between the controller and the view.
  1. What is the use of @ModelAttribute annotation?
  • The @ModelAttribute annotation in Spring MVC is used to bind method parameters or method return values to a named model attribute. It can be used to populate model attributes before invoking handler methods or to access model attributes in view templates.
  1. What is the purpose of @RequestMapping annotation?
  • The @RequestMapping annotation in Spring MVC is used to map web requests to specific handler methods in controllers. It allows developers to define the URL patterns and HTTP methods that should be handled by each method.
  1. How does Spring MVC support validation?
  • Spring MVC supports validation by providing integration with validation frameworks like Hibernate Validator or by implementing custom validation logic in controller methods using @Valid or @Validated annotations along with javax.validation.constraints annotations.
  1. What is the role of ModelAndViewResolver in Spring MVC?
  • ModelAndViewResolver in Spring MVC is responsible for resolving ModelAndView instances returned by controller methods to actual views. It allows for customizing the view resolution process in the application.
  1. Explain the differences between ModelAndView and ModelMap.
  • ModelAndView represents both a model and a view, whereas ModelMap is just a Map implementation used to pass data to the view. ModelAndView allows for specifying the view name explicitly, while ModelMap relies on the default view resolution mechanism.
  1. What is the purpose of @PathVariable annotation?
  • The @PathVariable annotation in Spring MVC is used to extract values from URI templates and bind them to method parameters in controller methods. It allows developers to access dynamic parts of the URL within the handler method.
  1. How can you handle file uploads in Spring MVC?
  • File uploads in Spring MVC can be handled using the MultipartResolver interface, which allows for parsing multipart requests containing file uploads. Controllers can then access uploaded files as MultipartFile objects.
  1. What is the purpose of @ResponseBody annotation?
    • The @ResponseBody annotation in Spring MVC is used to indicate that the return value of a controller method should be serialized directly to the HTTP response body. It is commonly used for returning JSON or XML responses in RESTful web services.

  1. How does Spring MVC support internationalization and localization?
  • Spring MVC supports internationalization and localization by allowing developers to define message bundles containing localized messages for different languages. These messages can be resolved and used in view templates using the MessageSource interface and by configuring locale resolution strategies.
  1. Explain the concept of interceptors in Spring MVC.
  • Interceptors in Spring MVC are components that intercept and process requests before they reach the controller or after the controller has processed the request but before the view is rendered. Interceptors are commonly used for tasks such as logging, authentication, or modifying the request/response.
  1. What is the use of @SessionAttributes annotation?
  • The @SessionAttributes annotation in Spring MVC is used to specify which model attributes should be stored in the HTTP session between requests. It allows for maintaining model attributes across multiple requests, typically used for form data or other user-specific information.
  1. How does Spring MVC support RESTful web services?
  • Spring MVC supports RESTful web services by providing features such as @RestController annotation for creating RESTful controllers, support for content negotiation, HTTP message converters for converting between Java objects and JSON/XML, and support for HTTP methods such as GET, POST, PUT, DELETE.
  1. Explain the role of HandlerInterceptor in Spring MVC.
  • HandlerInterceptor in Spring MVC is an interface that allows developers to intercept and modify requests and responses at different points in the request processing lifecycle. It provides methods for pre-processing and post-processing of requests, as well as for handling exceptions.
  1. What is the purpose of @ExceptionHandler annotation?
  • The @ExceptionHandler annotation in Spring MVC is used to define methods that can handle specific types of exceptions thrown during request processing. These methods can be used to customize error handling and return appropriate error responses to clients.
  1. How can you implement content negotiation in Spring MVC?
  • Content negotiation in Spring MVC can be implemented by configuring content negotiation strategies such as PathExtensionContentNegotiationStrategy or ParameterContentNegotiationStrategy. These strategies allow clients to request different representations of the same resource based on factors like file extension or request parameters.
  1. What is the role of RedirectAttributes in Spring MVC?
  • RedirectAttributes in Spring MVC is an interface used to pass attributes between redirecting and redirected requests. It allows for adding flash attributes that survive a redirect and can be accessed in the redirected request.
  1. How can you use @RequestBody annotation in Spring MVC?
  • The @RequestBody annotation in Spring MVC is used to bind the body of a HTTP request to a method parameter in a controller method. It is commonly used for handling JSON or XML payloads in RESTful web services.
  1. Explain the differences between forward and redirect in Spring MVC.
  • Forward in Spring MVC involves internally forwarding the request from one controller method to another within the same server request, while redirect involves sending a new request to a different URL. Forward maintains the original URL and request attributes, while redirect creates a new request and URL.

  1. What is the purpose of @ResponseStatus annotation?
  • The @ResponseStatus annotation in Spring MVC is used to declare the HTTP status code to be returned by a controller method in response to a specific exception. It allows developers to customize the status code returned for different types of exceptions.
  1. How does Spring MVC handle view resolution?
  • Spring MVC handles view resolution by using ViewResolver implementations to map logical view names returned by controllers to actual view implementations. ViewResolver implementations are configured in the application context and determine which view technology (e.g., JSP, Thymeleaf) to use.
  1. Explain the concept of Flash attributes in Spring MVC.
  • Flash attributes in Spring MVC are attributes that survive a redirect and are automatically made available as model attributes in the redirected request. They are typically used for passing data between requests, such as form submission success messages.
  1. What is the use of @CrossOrigin annotation?
  • The @CrossOrigin annotation in Spring MVC is used to enable cross-origin resource sharing (CORS) for specific controller methods or globally for all controller methods in a class. It allows web browsers to make cross-origin requests to the server.
  1. How can you configure multiple view resolvers in Spring MVC?
  • Multiple view resolvers can be configured in Spring MVC by declaring multiple ViewResolver beans in the application context and specifying their order using the mvc:view-resolvers element in XML configuration or by implementing the Ordered interface for programmatic configuration.
  1. What is the purpose of HandlerExceptionResolver in Spring MVC?
  • HandlerExceptionResolver in Spring MVC is an interface used to handle exceptions thrown during request processing. It allows developers to define custom exception handling logic, such as mapping specific exceptions to error views or returning custom error responses.
  1. How does Spring MVC support RESTful URL patterns?
  • Spring MVC supports RESTful URL patterns by allowing developers to define controller methods with @RequestMapping annotations specifying HTTP methods (e.g., GET, POST, PUT, DELETE) and URL patterns that correspond to RESTful resource endpoints.
  1. Explain the concept of ModelAndViewContainer in Spring MVC.
  • ModelAndViewContainer in Spring MVC is a container object that holds model attributes and view information during request processing. It is used to pass model attributes to views and to specify the view to be rendered as a response.
  1. What is the use of @RequestParam annotation?
  • The @RequestParam annotation in Spring MVC is used to bind request parameters to method parameters in controller methods. It allows developers to access query parameters or form parameters passed in the request URL or body.
  1. How can you handle AJAX requests in Spring MVC?
  • AJAX requests in Spring MVC can be handled by returning JSON or XML responses from controller methods using @ResponseBody annotation or by implementing specific controller methods to handle AJAX requests and return appropriate responses.

  1. What is the purpose of ContentNegotiatingViewResolver in Spring MVC?
  • ContentNegotiatingViewResolver in Spring MVC is used to resolve views based on content negotiation, allowing the application to serve different representations of the same resource (e.g., JSON, XML, HTML) based on the client’s preferences.
  1. Explain the role of MultipartResolver in Spring MVC.
  • MultipartResolver in Spring MVC is an interface used to handle multipart requests, such as file uploads. It allows controllers to access multipart/form-data requests and extract file uploads as MultipartFile objects.
  1. How can you handle session attributes in Spring MVC?
  • Session attributes in Spring MVC can be handled by using @SessionAttributes annotation to specify which model attributes should be stored in the HTTP session between requests. Controllers can then access session attributes using @ModelAttribute or by injecting HttpSession directly.
  1. What is the purpose of @MatrixVariable annotation?
  • The @MatrixVariable annotation in Spring MVC is used to extract matrix variables from the URI path and bind them to method parameters in controller methods. Matrix variables are a way of passing key-value pairs within a URI segment.
  1. How does Spring MVC support method-level security?
  • Spring MVC supports method-level security by integrating with Spring Security framework, allowing developers to secure controller methods using annotations such as @Secured, @PreAuthorize, @PostAuthorize, and @RolesAllowed.
  1. Explain the differences between RedirectView and RedirectAttributes.
  • RedirectView is a class in Spring MVC used to perform server-side redirects by sending an HTTP redirect response to the client, while RedirectAttributes is an interface used to pass attributes between redirecting and redirected requests.
  1. What is the use of @ResponseStatusException annotation?
  • The @ResponseStatusException annotation in Spring MVC is used to declare the HTTP status code and reason phrase to be returned by a controller method in response to a specific exception. It allows developers to customize the error response for different types of exceptions.
  1. How can you implement custom view resolvers in Spring MVC?
  • Custom view resolvers in Spring MVC can be implemented by creating classes that implement the ViewResolver interface and defining custom view resolution logic. These custom view resolvers can then be configured in the application context.
  1. Explain the concept of message converters in Spring MVC.
  • Message converters in Spring MVC are components responsible for converting between Java objects and HTTP request/response bodies in different formats, such as JSON, XML, or form-urlencoded. Spring MVC provides built-in message converters for common data formats.
  1. What is the purpose of @SessionAttribute annotation?
  • The @SessionAttribute annotation in Spring MVC is used to access session attributes directly within a controller method. It allows developers to retrieve session attributes by name without having to explicitly retrieve the HttpSession object.