Spring MVC - Interview Questions
What is the Spring MVC framework?
Spring MVC is the pristine web framework from Spring built on the Servlet API. It provides Model-View-Controller architecture that can be acclimated to develop flexible web applications. Spring MVC is one of the core components of the Spring framework. It comes with yare to utilize components and elements that avail developers to build flexible and robust web applications. As the denomination suggests, the MVC architecture disunites the different aspects of the application – input logic, business logic, and UI logic. It additionally provides a loose coupling between the M, V, and C of the application.
What are some benefits of Spring MVC framework over other MVC frameworks?
- Clear disseverment of roles – controller, validator, command object, form object, model object, DispatcherServlet, handler mapping, view resolver, etc. Each role can be consummated by a specialized object.
- Reusable business code – With Spring MVC, we don’t need to duplicate our code. We can utilize our existing objects.
- Customizable binding and validation
- Customizable locale and theme resolution
- Customizable handler mapping and view resolution
What is DispatcherServlet?
The job of the DispatcherServlet is to take an incoming URI and find the right cumulation of handlers (generally methods on Controller classes) and views (generally JSPs) that cumulate to compose the page or resource that's supposed to be found at that location.
What is the front controller class of the Spring MVC?
All the incoming requests are handled by the single servlet denominated DispatcherServlet which acts as the front controller in Spring’s MVC module.
What is the Role of the @Autowired Annotation?
The @Autowired annotation can be utilized with fields or methods for injecting a bean by type. This annotation sanctions Spring to resolve and inject collaborating beans into our bean.
Difference between @Controller and @RestController ?
The @Controller annotation has been part of the framework for a very long time.@Controller annotation designates that the annotated class is a controller. It is a specialization of @Controller and is autodetected through classpath scanning. It is typically utilized in amalgamation with annotated handler methods predicated on the @RequestMapping annotation.
The request handling method is annotated with @ResponseBody. This annotation enables automatic serialization of the return object into the HttpResponse.
On the other hand,
The @RestController annotation was introduced in Spring 4.0 to simplify the engendering of RESTful web services. It's a convenience annotation that combines @Controller and @ResponseBody.
The controller is annotated with the @RestController annotation, consequently, the @ResponseBody isn't required. Every request handling method of the controller class automatically serializes return objects into HttpResponse.
Difference between Spring MVC and Spring Boot?
Spring MVC is a project within the Spring Framework for implementing the model-view-controller design pattern. It is utilized to build dynamic (and static remotely) web pages and RESTful web services.
Spring Boot is an opinionated way of building Java applications. Utilizing Spring Boot, you can build web applications utilizing Spring MVC as well as other technologies.
What are stereotype annotations in Java?
Stereotype annotations are @Component, @Service, @Repository, and @Controller annotations. These annotations are used for auto-detection of beans using @ComponentScan and component-scan. The Spring stereotype @Component is the parent stereotype. The other stereotypes i.e @Service, @Repository, and @Controller are the specialization of @Component annotation.
What are the @RequestBody and the @ResponseBody?
@RequestBody: Annotation indicating a method parameter should be bound to the body of the HTTP request.
@ResponseBody: Annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).
Alternatively, we can use @RestController annotation in place of @Controller annotation. This will remove the need to using @ResponseBody.
What is the purpose of @qualifier annotation?
The @Qualifier annotation is utilized to resolve the autowiring conflict, when there are multiple beans of same type.
The @Qualifier annotation can be utilized on any class annotated with @Component or on method annotated with @Bean. This annotation can withal be applied on constructor arguments or method parameters.
What is the purpose of @Required annotation?
The @Required annotation in spring is a method-level annotation applied to the setter method of a bean property and thus making the setter-injection mandatory. This annotation denotes that the required bean property must be injected with a value at the configuration time.
Difference between @Component, @Controller, @Repository & @Service annotations?
- The @Component annotation marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context.
- The @Repository annotation is a specialization of the @Component annotation with kindred use and functionality. In additament to importing the DAOs into the DI container, it additionally makes the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException
- The @Service annotation is withal a specialization of the Component annotation. It doesn’t currently provide any supplemental behavior over the @Component annotation, but it’s a good conception to utilize @Service over @Component in service-layer classes because it designates intent better.
- @Controller annotation marks a class as a Spring Web MVC controller. It too is a @Component specialization, so beans marked with it are automatically imported into the DI container. When you integrate the @Controller annotation to a class, you can utilize another annotation i.e. @RequestMapping; to map URLs to instance methods of a class.
What Is a MultipartResolver and When Should We Use It?
The MultipartResolver interface is used for uploading files.
What Is Spring MVC Interceptor?
Spring Interceptor is a concept that is rather similar to Servlet Filter. Spring Interceptor is used to intercept client requests and process them. Sometimes we want to intercept the HTTP Request and do some processing before handing it over to the controller handler methods.
What Does the @ExceptionHandler Annotation Do?
The @ExceptionHandler annotation sanctions us to define a method that will handle the exceptions. We may utilize the annotation independently, but it's a far better option to utilize it together with the @ControllerAdvice. Thus, we can establish an global error handling mechanism.