@RestController in SpringBoot

  • Spring RestController annotation is a convenience annotation that is itself annotated with @Controller and @ResponseBody. This annotation is applied to a class to mark it as a request handler.
  • Spring RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method.
  • Once response body is generated from the handler method, it converts it to JSON or XML response.
  • We have configured our REST application to work with both XML and JSON.So how it will know that whether the request is XML or JSON. And if the response should be sent in JSON or XML format.This is where Accept and Content-Type Request Headers are used.

Content-Type: Defined the type of content in request body, if its value is “application/xml” then Spring will treat request body as XML document. If its value is “application/json” then the request body is treated as JSON.

Accept: Defined the type of content client is expecting as response. If its value is “application/xml” then XML response will be sent. If its value is “application/json” then JSON response will be sent.

Leave a Comment