11 Spring MVC Interview Q&As

Q1. Can you describe the Spring Web MVC framework architecture?
A1. Spring Web MVC framework provides a Model View Controller architecture to develop loosely coupled 1) web applications 2) RESTful web services. Here is the high level diagram.

Spring MVC Flow

Step 1: All requests are handles by the Spring DispatcherServlet, which applies the Front Controller design pattern.

Steps 2, 3, 4, 5 & 6: Spring Handler Mapping will find the appropriate Controller classes, which are annotated via @Controller for web applications and @RestController for the RESTful web services. The controller executes the request and creates a Model and stores it in a relevant scope like Request, Session, etc. Finally, selects a View if it is web application or sends the model data as XML or JSON back to the client (e.g. Browser) if its a RESTful web service.

Steps 7, 8, 9 & 10: If it is web application the DispatcherServlet resolves the view name and redirects to the view template (e.g. JSP, Thymeleaf, Freemarker, etc). The response html is returned to DispatcherServlet, and then to the browser.

This is covered in detail: Spring MVC beginner tutorial step by step | ⏯ Spring MVC beginner video tutorial step by step.

Q2. What are the advantages of Spring MVC?
A2. 1) Spring provides a very clear separation of controllers, Java Bean models, and view technologies like JSPs, Thymeleaf, Velocity, FreeMarker, etc. You can also roll out your own custom templating language by implementing the Spring View interface. Just about every part of MVC is configurable by plugging in an implementation to its interface, and thanks to Spring’s adherence to the OOP design principle “open/closed principle (OCP)“.

2) Spring Controllers are configured via IoC, making it easier to unit test. Spring Controller Unit Testing.

3) Controller can either select a view name and prepare model map for it or write the response directly to response stream in XML, JSON, Atom, and many other types of content to enable RESTful web services. Spring 4 MVC RESTful Web Service Beginner Tutorial step by step.

Q3. What are some of the important annotations?
A3. Refer to the Spring MVC tutorials for details.

@Controller : This class will serve as a controller.

@RestController : This class will serve as a controller to service RESTful web services. Under the hood, a “@RestController = @Controller + @ResponseBody”.

@RequestMapping : Maps URI path to a class or method depending on defined at the class or method level.

@PathVariable : Used to map a dynamic value in the URI to a method argument.

@RequestBody : Spring will bind the incoming HTTP request body in XML, JSON, etc based on the Accept header.

@ResponseBody : Spring will bind the return value to outgoing HTTP response body. Spring will use HTTP Message converters to convert to XML, JSO, etc.

Q4. What is a “ResponseEntity” in Spring MVC?
A4. ResponseEntity is used to represent an entire “HTTP response“.

Q5. What is an “HttpMessageConverter” in Spring MVC?
A5. An HttpMessageConverter is responsible for marshall & unmarshall Java POJO objects like Account, User, etc to and from JSON, XML, etc over HTTP(s). The Spring MVC applications need to be annotated with “@EnableWebMvc“.

Spring is configured out-of-box for many default HttpMessageConverters depending on presence of certain libraries in the project classpath. For example, add the following libraries to work with XML & JSON conversions. If the Content-Type in request header was one of application/json or application/xml, and the relevant libraries shown below are in the claspath, then Spring will delegate the conversion to MappingJackson2HttpMessageConverter for JSON or MappingJackson2XmlHttpMessageConverter for XML.

The “HttpMessageConverter” is a good example of the “Strategy design pattern” with different implementations like “MappingJackson2HttpMessageConverter” to read and write JSON using Jackson’s ObjectMapper, and “MappingJackson2XmlHttpMessageConverter” to read and write XML using Jackson XML extension’s XmlMapper, “StringHttpMessageConverter” to read and write Strings from the HTTP request and response, “FormHttpMessageConverter” to read and write form data from the HTTP request and response, and so on. You can also create your own custom implementation of the “HttpMessageConverter”.

Q6. What are interceptors in Spring MVC?
A6. Handler interceptors are used when you want to apply specific behaviour to certain requests. Handler Interceptors should implement the interface “HandlerInterceptor” to provide specific behaviours via “preHandle(..)”, “postHandle(..)”, and “afterCompletion(..)” methods.

You can also extend

finally the Java Configuration class will have an entry as

Q7. How do you validate form data in Spring MVC?
A7. Spring MVC supports validation by means of a validator object that implements the Validator interface. You need to create a class and implement the Validator interface.

Q8. How do you handle exceptions in Spring MVC?

To apply to entire application – Spring Java Config
To apply to a specific handler method with @ExceptionHandler annotation

Q9. How do you enable security in Spring MVC?
A9. Spring Security is used to implement Authentication and Authorization for a web application.

Spring Security dependencies jars
Configure Security
Configure Spring Web with SecurityConfiguration class

Q10. How do you achieve localization in Spring MVC?
A10. LocaleResolver is shipped with Spring to support Internationalization (i18n) and Localization (L10n).

JSP View
Spring Java Configuration

Q11. How do you get handles on ServletContext and ServletConfig objects Spring MVC?
A11. @Autowired annotation to the rescue.

Java Interview FAQs

800+ Java Interview Q&As