0% found this document useful (0 votes)
4 views2 pages

@Controller vs. @RestController Explained

The document explains the difference between @Controller and @RestController in Spring MVC, highlighting that @RestController combines @Controller and @ResponseBody, automatically converting return values to HTTP response bodies. It provides code examples for both annotations, demonstrating how @Controller is used to create a model and view, while @RestController directly returns objects as JSON or XML. The document also includes the annotations' declarations and their usage in handling HTTP requests.

Uploaded by

Kiran Sardesai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

@Controller vs. @RestController Explained

The document explains the difference between @Controller and @RestController in Spring MVC, highlighting that @RestController combines @Controller and @ResponseBody, automatically converting return values to HTTP response bodies. It provides code examples for both annotations, demonstrating how @Controller is used to create a model and view, while @RestController directly returns objects as JSON or XML. The document also includes the annotations' declarations and their usage in handling HTTP requests.

Uploaded by

Kiran Sardesai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

@Controller vs.

@RestController
The key difference between @Controller vs. @RestController is how the HTTP
response is getting created.
In Spring MVC, @RestController = @Controller + @ResponseBody
Any method annotated with @ResponseBody, spring automatically convert the
return value and written into HTTP response body
@Controller is to create a Map of model object and find a view
@Controller
@RequestMapping("customer")
public class Customer {
Customer customer = new Customer ();
@RequestMapping(value = "/{name}", method = [Link], produce
s = "application/json")
public @ResponseBody Customer getCustomer(@PathVariable String name) {
[Link](name);
[Link]("test@[Link]");
return customer;
}
}

@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Component
public @interface Controller

Using @RestController
@RestController simply returns the object and object data is directly written into
HTTP response as JSON or XML.
Spring v4.0 onwards
@RestController
@RequestMapping("customer")
public class Customer {
Customer customer = new Customer ();
@RequestMapping(value = "/{name}", method = [Link], produce
s = "application/json")
public Customer getCustomer(@PathVariable String name) {
[Link](name);
[Link]("test@[Link]");
return customer;
}
}

Declaration of @RestController looks like


@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController

You might also like