What is @restcontroller annotation in spring boot

what is @restcontroller annotation in spring boot: @RestController is an annotation in Spring Boot that is used to create a RESTful web service. This annotation is a combination of two annotations, @Controller and @ResponseBody.

@Controller annotation is used to define a class as a Spring MVC controller, while @ResponseBody annotation is used to bind the returned object to the HTTP response body. By using the @RestController annotation, you can eliminate the need for using both @Controller and @ResponseBody annotations.

In other words, the @RestController annotation is used to create a RESTful web service that maps HTTP requests to methods that perform the requested operation and return JSON or XML data. The data returned from the RESTful web service can be consumed by clients like web browsers and mobile applications.

Here is an example of a RESTful web service created using the @RestController annotation in Spring Boot:

@RestController
@RequestMapping("/api")
public class MyRestController {

   @GetMapping("/hello")
   public String sayHello() {
       return "Hello, World!";
   }
}

In this example, we have defined a class called MyRestController with the @RestController annotation. The @RequestMapping annotation is used to map the URL “/api” to this controller.

We have also defined a method called “sayHello” with the @GetMapping annotation, which maps the URL “/api/hello” to this method. When this method is called, it returns a string “Hello, World!”, which is automatically converted to JSON and sent as the response body.

Overall, the @RestController annotation is a convenient way to create RESTful web services in Spring Boot, and it saves you from writing extra code for mapping the HTTP response body to Java objects.

difference between @controller and @restcontroller

Both @Controller and @RestController are annotations in Spring Boot that are used to handle web requests. However, there are some differences between them:

  1. Purpose: The @Controller annotation is used to create a Spring MVC controller that returns a view, while the @RestController annotation is used to create a RESTful web service that returns data in the form of JSON, XML, or other formats.
  2. Response type: In @Controller, the response is typically an HTML view, while in @RestController, the response is typically a serialized object, such as JSON or XML.
  3. @ResponseBody annotation: In @Controller, you need to use the @ResponseBody annotation to indicate that the method returns data that should be written directly to the response body. In @RestController, this annotation is implicitly added to all methods, and it returns data directly as the response body.
  4. Convenience: Using the @RestController annotation is a convenient way to create RESTful web services, as it eliminates the need to use both the @Controller and @ResponseBody annotations.

Here’s an example that demonstrates the difference between @Controller and @RestController:

javaCopy code@Controller
@RequestMapping("/hello")
public class MyController {

   @GetMapping("/")
   public String sayHello() {
       return "hello";
   }
}

@RestController
@RequestMapping("/api")
public class MyRestController {

   @GetMapping("/hello")
   public String sayHello() {
       return "Hello, World!";
   }
}

In this example, the MyController class is annotated with @Controller and returns a view, while the MyRestController class is annotated with @RestController and returns data directly as the response body.

When you access the URL “/hello” mapped to the MyController class, it will return an HTML view with the content “hello”. When you access the URL “/api/hello” mapped to the MyRestController class, it will return the string “Hello, World!” as the response body, which can be consumed by a REST client.

Leave a Comment