0% found this document useful (0 votes)
2 views

Spring MVC and Rest

The document provides an overview of the MVC (Model-View-Controller) design pattern and its implementation in Spring MVC, detailing the roles of the Dispatcher Servlet, annotations like @Controller, @RestController, @RequestMapping, and @ResponseBody, as well as HTTP status codes. It explains how to map controller methods to URLs, handle exceptions, and the differences between various annotations for request and response handling. Additionally, it highlights the use of @EnableWebMvc for enabling Spring MVC and the distinctions between related annotations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Spring MVC and Rest

The document provides an overview of the MVC (Model-View-Controller) design pattern and its implementation in Spring MVC, detailing the roles of the Dispatcher Servlet, annotations like @Controller, @RestController, @RequestMapping, and @ResponseBody, as well as HTTP status codes. It explains how to map controller methods to URLs, handle exceptions, and the differences between various annotations for request and response handling. Additionally, it highlights the use of @EnableWebMvc for enabling Spring MVC and the distinctions between related annotations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1: What is MVC?

 The MVC(Model-View-Controller) is a software design pattern.


 It separates the functionality of an application into three inter connected parts - Model, View
and controller.
2 - What is the Front controller of spring MVC?

 The front controller is a Dispatcher Servlet class present in


org.springframework.web.servlet.package.
 It dispatches the request to the appropriate controller and manages the flow of the
application.
 It is required to specify the Dispatcher Servlet class in the web.xml file.
3: Explain the flow of spring MVC?

 Once the request has been generated the Dispatcher Servlet that works as the front
controller.
 The Dispatcher Servlet gets an entry of handler mapping from the XML file and forwards the
request to the controller.
 The controller returns an object of Model and View.
 The Dispatcher Servlet checks the entry of view resolver in the XML file and invokes the
specified view component.

4: Explain the difference b/w @Conroller and @RestController?

 The main difference between the @controller and @Restcontroller annotation is that the
@ResponseBody annotation is automatically included in the @Restcontroller.
 This means that we don’t need to annotate our handler methods with the @ResponseBody if
we used @Restcontroller.
 We need to do this in a @Controller class if we want to write response type to the HTTP
response body

5: What are the @RequestBody and the @ResponseBody Annotations?

 Both the annotation is binding http Request and http Response with our domain object.
 @RequestBody annotation we are used when we sent any data to the server.
 Mainly we are used @RequestBody annotation on post and put method.
 The @RequestBody annotation allows us to retrieve the requests body and automatically
convert it to Java Object
 @ResponseBody tell Spring framework to serialize a return object into JSON or XML and
send this information back as part of the HTTP Response.

6: How to map controller class and its methods with URL?

 The @RequestMapping annotation is used to map the controller class and its methods.
 we can specify this annotation on the class and method level.

7: What is the purpose of @Pathvariable annotation in spring MVC?

 Suppose we re getting value from db. with some exact value like id, name in that case in
method parameter we are using @Pathvarible and in method URL we need to pass that
value like /show/{id}
8: What is the role of @ResponseBody annotation in spring MVC?

 @ResponseBody tell Spring framework to serialize a return object into JSON or XML and
send this information back as part of the HTTP Response.

9: Which anno are used to define Global Exception Handler class?

 @ControllerAdvice (or) @RestControllerAdvice along with @ExceptionHandler


 We need to define one user class with annotation @ControllerAdvice or
@RestControllerAdvice
 Then Define one method and @ExceptionHandler with Exception Type.

10: Explain some Response Status codes?

200: Ok success

400: Bad Request- input data is invalid

401: unauthorized- login request is failed

403: forbidden - User not allowed to access resource

404: Not found- URL is not exist

405- Method Not Allowed - Request is invalid

500-Internal server error

11: Where do you need @EnableWebMVC?

 The @EnableWebMvc annotation is used for enabling Spring MVC in an application and
works by importing the Spring MVC Configuration from WebMvcConfigurationSupport. The
XML equivalent with similar functionality is <mvc:annotation-driven/>.

12: What is the HTTP status return code for a successful DELETE statement?

 Responses. If a DELETE method is successfully applied, there are several response status
codes possible: A 202 (Accepted) status code if the action will likely succeed but has not yet
been enacted. A 204 (No Content) status code if the action has been enacted and no further
information is to be supplied.

13: When do you need @ResponseBody annotation in Spring MVC?

 The @ResponseBody annotation tells a controller that the object returned is automatically
serialized into JSON and passed back into the HTTP Response object. When you use the
@ResponseBody annotation on a method, Spring converts the return value and writes it to
the HTTP response automatically.

14: What does @RequestMapping annotation do?

 The @RequestMapping annotation is used to map requests to controllers’ methods. It has


various attributes to match by URL, HTTP method, request parameters, headers, and media
types. You can use it at the class level to express shared mappings or at the method level to
narrow down to a specific endpoint mapping.

15: What is @QueryParm?


16: What is difference B/w @Requestmaping and @getmapping?

 The key difference between the two is that @RequestMapping is used for all kind of HTTP
methods while @GetMapping offers a specialized, succinct, and intuitive approach for
handling GET requests. While they share the core functionality of routing requests, they
cater to different scenarios and developer preferences.

17: what is option method is rest Api?

 The OPTIONS method represents a request for information about the communication
options available on the request/response chain identified by the Request-URI.

18: Difference b/w @queryParam and @requestParam?

 @QueryParam is a JAX-RS framework annotation and @RequestParam is from Spring.


QueryParam is from another framework and you are mentioning Spring. @Flao wrote that
@RequestParam is from Spring and that should be used in Spring MVC.

You might also like