Spring MVC Interview Questions with Answers


Sep 13, 2021

Spring MVC Interview Questions

This post is the piece of our Interview Questions series and in this post, we will examine some generally asked Spring MVC interview questions during a prospective employee meeting.

Q1. What is a Controller in Spring MVC?

Regulators control the progression of the application execution. In Spring MVC design the DispatcherServlet functions as Front Controller. DispatcherServlet measure the solicitation and pass the solicitation to the regulator class commented on with @Controller. Every regulator class is dependable to deal with at least one solicitations of a particular kind.


Q2. How to get ServletContext and ServletConfig in a Spring Bean?

There are two alternatives for getting ServletContext and ServletConfig in a Spring Bean.

Use @Autowired comment to infuse ServletContext and ServletConfig in Spring Bean.

Carry out Spring mindful interfaces in the class that relies upon ServletConfigor ServletContext

Utilizing @Autowired comment


Carrying out Spring mindful interfaces

public class ServletConfigAwareClass carries out ServletConfigAware {
private ServletConfig config;
public void setServletConfig(ServletConfig servletConfig) {
this.config = servletConfig;


Q3. Clarify the working of @RequestMapping?

The @RequestMapping is utilized to plan web solicitation to the regulator in Spring MVC application. We can apply the @RequestMapping explanation to class-level as well as technique level in a regulator.

Q4. What is ViewResolver in Spring?

The ViewResolver let SPring MVC application to deliver models in a program without restricting to a particular view innovation (for example JSP or Thymeleaf). This works by giving a planning between see names and genuine views.View tends to the readiness of information prior to giving up to a particular view innovation.

Q5. What is Spring MVC Interceptor and how to utilize it?

Interceptors are valuable to block the customer solicitation and cycle it. There are three choices to block customer demand utilizing Spring interceptors.



afterComplete (after the view is delivered to the customer)

Interceptors are valuable for cross-cutting concerns and assist us with staying away from copy code (for example logging, profiling etc.).We can make spring interceptor by executing a HandlerInterceptor interface or by expanding the theoretical class HandlerInterceptorAdapter.

Q6. How to deal with special cases in Spring MVC?

Spring MVC gives the accompanying three alternatives to exemption taking care of

@ExceptionHandler Annotation – ExceptionHandler is a Spring comment handle exemptions tossed in response to popular demand taking care of. This comment works at the @Controller level.

<em><strong>@ControllerAdvice</strong></em> Annotation – This comment upholds worldwide Exception controller instrument. A regulator counsel permits you to utilize the very same exemption dealing with procedures yet applies them across the application, not simply to a singular regulator.

HandlerExceptionResolver – Resolve any exemption tossed by the application.


Q7. How to approve structure information in Spring Web MVC?

Spring offers worked in help to utilize JSR-303 based bean approval. For utilizing JSR-303 based approval, we need to clarify bean factors with the necessary approvals. How about we take a basic guide to approve client object

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Customer {
@Size(min = 2, max = 30)
private String name;
private Integer age;
/getter and setters

To trigger JSR-303 based approval, we just need to comment on our approaching item with a @Validannotation

public class WebController {
public String checkPersonInfo(@Valid Customer client, bindingResult) {
in the event that (bindingResult.hasErrors()) {
return "structure";
return "divert:/results";

Spring approval likewise gives an interface that can make custom validators (for a situation out of the container validator doesn't fulfill the necessities.)

Q8. What is MultipartResolver? When would it be advisable for us to utilize this?

We utilize this interface to transfer records. Spring Framework gives two executions to this interface

CommonsMultipartResolver – For Apache Commons FileUpload.

StandardServletMultipartResolver – For the Servlet 3.0+ Part API

We can utilize both of this to execute document transfer include in our application.

Q9. What is DispatcherServlet?

DispatcherServlet is the Spring MVC execution of the front regulator design. This is the focal passage point in the Spring MVC application or HTTP demand overseers/regulators. It's a servlet that takes the approaching solicitation, and agents preparing of that solicitation to one of a few overseers, the planning of which is indicated in the DispatcherServlet arrangement.


Q10. How to transfer a record in Spring MVC Application?

Spring Boot gives "CommonsMultipartResolver" and "StandardServletMultipartResolver" as two standard approaches to transfer a record. To transfer a record utilizing "CommonsMultipartResolver", we need to add the house fileupload.jar and lodge io.jar conditions

<!- - Apache Commons Upload - >
<!- - Apache Commons Upload - >

At long last, you need to compose a regulator to acknowledge approaching record and store information likewise. Technique boundary ought to have MultipartFile as one boundary. Here is the manner by which your code resemble

public class UploadController {
public String handleUpload(
@RequestParam(value = "document", required = bogus) multipartFile,
httpServletResponse) {
String orgName = multipartFile.getOriginalFilename();
/document saving rationale

You may Also Read: Most Asked Apache Spark Interview Questions

Q11. How to empower limitation support in Spring MVC?

Spring system gives LocalResolver to deal with internationalization and restriction. To empower support for the confinement in your application, we need to enroll under two beans.

SessionLocaleResolver: LocaleResolver execution that utilizes a district property in the client's meeting in the event of a custom setting, with a fallback to the predefined default region or the solicitation's acknowledge header area.

LocaleChangeInterceptor : Interceptor that takes into account changing the current district on each solicitation, through a configurable solicitation boundary (default boundary name: "region").

public LocaleResolver localeResolver(){
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
return localeResolver;
public localeChangeInterceptor() {
localeChangeInterceptor = new LocaleChangeInterceptor();
return localeChangeInterceptor;


Q12. What is ContextLoaderListner in Spring MVC and what is the utilization of this?

The ContextLoaderListener is the root application setting. The following are the fundamental employments of the ContextLoaderListener.

It assists with restricting the ApplicationContext and ServletContext lifecycle.

It assists with making ApplicationContext naturally without the need of any extra code.

</audience class>
</setting param>


Q13. How to utilize JNDI DataSource in Spring Web Application?

There are numerous approaches to utilize the JNDI datasource given by Tomcat. We can utilize the Spring XML based designs to get the datasource.

<beans xmlns="http://www.springframework.org/composition/beans"
xmlns:jee="http://www.springframework.org/composition/jee" xsi:schemaLocation="
http://www.springframework.org/composition/beans http://www.springframework.org/diagram/beans/spring-beans.xsd
http://www.springframework.org/composition/jee http://www.springframework.org/diagram/jee/spring-jee.xsd">
<jee:jndi-query id="dataSource"
anticipated type="javax.sql.DataSource"/>

Another way (Preferred) is to design the information source as a Spring Bean

<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/TestDB"/>
The last alternative is to pronounce the JNDI arrangement in the server.xml record and use it in the Tomcat's context.xml.For more detail, kindly read JNDI Datasource HOW-TO

Q14. What is Viewresolver design in Spring MVC and how can it function?

View resolver is a J2EE configuration design which permits applications to powerfully pick a view innovation. Spring gives see resolvers execution to help passing data to the program without restricting to a particular view innovation. In Spring MVC, control returns the name of the view which is passed to the view resolver in Spring MVC for picking the right templating motor. The two interfaces critical to the manner in which Spring handles sees are ViewResolver and View.

Q15. What is the extent of Spring Controller?

Spring MVC regulators are singleton as a matter of course. If it's not too much trouble, note it will share any article made in the regulator across all solicitations.

Q16. Clarify Model Attribute

This is one of the most significant and regularly utilized explanation in Spring MVC.The @ModelAttribute comment that ties a strategy boundary or technique return worth to a named model characteristic, presented to a web view.Spring MVC will conjure all strategies with @ModelAttribute comment before any overseer technique executed by the system.

A @ModelAttribute on a strategy contention shows the contention ought to be recovered from the model. If not present in the model, the contention ought to be started up first and afterward added to the model.We can utilize @ModelAttribute explanation at technique and strategy boundary level.Read our article Spring MVC @ModelAttribute Annotation for more detail.

In this article, we talked about some usually asked Spring MVC interview questions during a new employee screening. If it's not too much trouble, read Spring Interview Questions for the rundown of Spring center inquiries.




"Drupal is a free software package that allows an individual or a community of users to easily publish, manage, and organize a wide variety of content on a web site."