Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Spring MVC

Description

Spring MVC Inteview questions
Jerry Reeves Jr
Flashcards by Jerry Reeves Jr, updated more than 1 year ago
Jerry Reeves Jr
Created by Jerry Reeves Jr over 2 years ago
1
0
1 2 3 4 5 (0)

Resource summary

Question Answer
What is DispatcherServlet in the Spring MVC application? In the case of Spring MVC, DispatcherServlet is the front controller. DispatcherServlet acts as an entry and exit point for any request received by the rom client.
How DispatcherServlet handle requests &responses in the Spring MVC application? Whenever a request comes it first goes to the DispatcherServlet where it then tries to identify its handler method (the methods defined in the specific controller to handle the requests) using Handler mapping. Once the handler mapping returns the controller the DispatcherServlet knows the controller which can handle the request and goes there for further request processing. Once the controller returns the view the DispatcherServlet goes to the view resolver to identify where the view is located. DispatcherServlet then grabs the view and returns as the final l response.
How Spring MVC DispatcherServlet is registered in the web.xml file? Since DispatcherServlet is one type of Servlet the web.xml file configuration is the same as normal servlet. Additionally, as DispatcherServlet is our front controller we need to ensure that all the incoming requests should be routed to it using "/" url pattern. If we are using the spring-boot-starter-web starter, DispatcherServletauto-configured to the URL pattern "/". So, we don't need to do any additional configuration in the web.xml file.
How to change the default context path URL pattern of DispatcherServlet in Spring MVC? It's very simple, we need to change two properties inside the application.properties file. With the above customizations, DispatcherServlet is configured to handle the URL pattern /v2 and the rcontext Path will be /admin. Thus, DispatcherServlet listens at http://localhost:8080/admin/v2/.
Can we have one @RestController class with multiple @GetMapping methods? Yes, we can have one @RestController class with multiple @GetMapping methods. Defining not only Get but any HTTP method-compliant mappings purely depend on the context of the application and its use cases. Below three GetMappings can be defined inside one UserRestController.
How do you ensure both "/" and "/welcome" request mappings land in the "index" view in Spring MVC applications? @RequestMapping annotation in Spring MVC has a String[] value parameter, so we can specify multiple values like the below to return the index view of the rom controller class
Can @Controller annotation be used for both Spring MVC and RESTful applications? Yes, @RestController is a convenience annotation that does nothing more than the @Controller and @ResponseBody annotations. Hence the following two controller definitions are the same
What does @Controller & @RestController returns? @Controller returns a view in the Spring MVC application. @RestController returns an object as a response instead of a view.
What is the use of @ResponseBody in Spring? @ResponseBody is a Spring annotation which binds a method return value to the web response body. It is not interpreted as a view name. It uses org.springframework.http.converter Interface HttpMessageConverter<T> to convert the return value to the HTTP response body, based on the content type in the request HTTP header.
What are MIME Types? MIME stands for Multi-purpose Internet Mail Extensions. MIME types form a standard way of classifying file types on the Internet. Internet programs such as Web servers and browsers all have a list of MIME types so that they can transfer files of the same type in the same way, no matter what operating system they are working in. A MIME type has two parts: a type and a subtype. They are separated by a slash (/) i.e., type/subtype. For example, the MIME type for Microsoft Word files is an application and the subtype is ms-word. Together, the complete MIME type is application/ms-word. The entire list of MIME types is available under Internet Assigned Numbers Authority (IANA) website- https://www.iana.org/assignments/media-types/media-types.xhtml The MIME types & extensions can be found under-https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
What is the use of @RequestBody in Spring? @RequestBody annotation request body to method parameters. We use the @RequestBody annotation to have the request body read and deserialized into an Object through an HttpMessageConverter. Additionally, automatic validations can be applied by annotating the argument with @Valid annotation.
What is the meaning of Content Negotiation? Content negotiation is the process of selecting one of the multiple possible representations to return to a client, based on client or server preferences. When a consumer sends a request, it can specify two HTTP Headers related to Content Negotiation Accept and Content-Type. Content-Type indicates the content type of the body of the request. Accept indicates the expected content type of the response.
What is RESTprotocol-dependentependent? REST is about resource state manipulation through their representations at the top of stateless communication between client and server. It's a protocol-independent architectural style but, in practice, it's commonly implemented on top of the HTTP protocol.
What are “representation", "state" and "transfer" in Representational State Transfer (REST)? To understand REST let us first understand the- What is a resource- The key abstraction of information in REST is a resource. There is no restriction on what a resource can be. Any information that can be named can be a resource: a document or image, a temporal service (e.g., "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g., a person), and so on. What is a representation- A JSON document can be used to represent the state of a particular resource. A resource can have many representations, such as JSON and/or XML documents, and the client can use content negotiation to request different representations of the same resource. What is a state transfer- The state of a given resource can be retrieved and manipulated using representations.
What is REST API versioning? API versioning is the process of transparently managing changes to your API. Versioning aims at effective communication around changes to API, so consumers/subscribers know what to expect from it.
How do we provide a version to RESTful APIs in Spring? APIs only need to be up-versioned when a breaking change is made. Breaking changes include: Change in the format of the response data for one or more calls Change in the request or response type (i.e., changing an integer to a float) Removing any part of the API. There are multiple ways to version RESTful API- One controller class with multiple methods having separate versions for mapping URLs. One controller class with one method having separate versions number passed as path variables. One controller class with one method having separate versions number passed as a custom request header. Multiple controller classes marked with version names with their method names. Breaking changes should always result in a change to the major version number for an API or content response type. Non-breaking changes, such as adding new endpoints or new response parameters, do not require a change to the major version number.
Which object is used as a base context in the Spring MVC application? Why? WebApplicationContext object is used as the base object in the Spring MVC application. WebApplictionContext is an extension of ApplicationContext public interface WebApplicationContext extends ApplicationContext. WebApplicationContext is a web-aware ApplicationContext i.e., it has Servlet Context information. In one web application, there can be multiple WebApplicationContext. In one web application, there can be multiple DispatcherServlet, one for handling request and returns view whereas another which handle REST request & responses. Each DispatcherServlet is associated with a single WebApplicationContext. The WebApplicationContext configuration file *-servlet.xml is specific to the DispatcherServlet and a web application can have more than one DispatcherServlet configured to handle the requests and each DispatcherServlet would have a separate *-servlet.xml file to configure.
What is the difference between applicationContext.xml and *-servlet.xml in Spring Framework? applicationContext.xml defines the beans that are shared among all the servlets. If our application has more than one servlet, then defining the common resources in the applicationContext.xml would make more sense. *-servlet.xml defines the beans that are related only to specific DispatcherServlet. All our Spring MVC controllers are defined in this file. There is nothing wrong in defining all the beans in the *-servlet.xml if we are running only one DispatcherServlet in our web application.
Show full summary Hide full summary

0 comments

There are no comments, be the first and leave one below:

Similar

Application of technology in learning
Jeff Wall
Innovative Uses of Technology
John Marttila
The Internet
Gee_0599
Ch1 - The nature of IT Projects
mauricio5509
CCNA Answers – CCNA Exam
Abdul Demir
SQL Quiz
R M
Professional, Legal, and Ethical Issues in Information Security
mfundo.falteni
System Analysis
R A
Flash Cards Networks
JJ Pro Wrestler
EDUC260- Multimodal Literacies for a Digital Age
angelwoo2002