Main Tutorials

Spring @Controller and @RestController Annotations

This article shows you what @Controller and @RestController are and their differences.

Table of contents:

1. Spring @Controller annotation

Spring 2.5 introduced the @Controller as the web controller for the MVC architecture. Spring 3.0 introduced the @ResponseBody to return the method’s return type directly into the HTTP response body. We can combine both @Controller and @ResponseBody to quickly create RESTful web services.

For example:

HelloController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

  @RequestMapping(value="/hello1", method= RequestMethod.GET)
  @ResponseBody
  String hello1() {
      return "Hello World! - 1";
  }

  @RequestMapping(value="/hello2", method= RequestMethod.GET)
  @ResponseBody
  String hello2() {
      return "Hello World! - 2";
  }

  @RequestMapping(value="/hello3", method= RequestMethod.GET)
  @ResponseBody
  String hello3() {
      return "Hello World! - 3";
  }

}

2. Spring @RestController annotation

Since the RESTful architecture is getting popular, and a class can easily contain a lot of @ResponseBody methods, generating many duplicated codes. So, Spring 4.0 introduced @RestController to simplify the creation of RESTful web services.

Review the @RestController source code; the @RestController itself is also annotated with @Controller and @ResponseBody.

RestController.java

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {

  @AliasFor(annotation = Controller.class)
  String value() default "";

}

Note
The @RestController does nothing but act like a shortcut for @Controller and @ResponseBody; it saves the developer from annotating @ResponseBody on all the RESTful handler methods.

With @RestController, now we can rewrite the above @Controller example like this:

HelloController.java

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

  @RequestMapping(value="/hello1", method= RequestMethod.GET)
  String hello1() {
      return "Hello World! - 1";
  }

  @RequestMapping(value="/hello2", method= RequestMethod.GET)
  String hello2() {
      return "Hello World! - 2";
  }

  @RequestMapping(value="/hello3", method= RequestMethod.GET)
  String hello3() {
      return "Hello World! - 3";
  }

}

3. Download Source Code

4. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments