Spring MVC and Rest
Spring MVC and Rest
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.
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
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.
The @RequestMapping annotation is used to map the controller class and its methods.
we can specify this annotation on the class and method level.
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.
200: Ok success
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.
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.
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.
The OPTIONS method represents a request for information about the communication
options available on the request/response chain identified by the Request-URI.