Now that, you are familiar with both of these annotations, it's a good time to analyze some factual difference between @RestController and @Controller. This is a very important concept, not just from the Interview point of view but also from Spring Core Developer Certification.

If you are preparing for Spring certifications, you should familiar with such subtle differences. Additionally, you can also take a look at free Spring tests to get an idea about exam format and level of questions.

Anyway, let's get back to the point, here are some important differences between these two annotations.

1. The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody.

2. The @RestController is relatively new, added only on Spring 4.0 but @Controller is an old annotation, exists since Spring started supporting annotation, officially it was added on Spring 2.5 version.

3. The @Controller annotation indicates that the class is a "Controller" like a web controller while @RestController annotation indicates that the class is a controller where @RequestMapping methods assume @ResponseBody semantics by default i.e. servicing REST API.

 

4. The @Controller is a specialization of @Component annotation while @RestController is a specialization of @Controller annotation. It is actually a convenience controller annotated with @Controller and @ResponseBody as shown below.

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

and here is how the declaration of @Controller looks like:

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


5. One of the key difference between @Controler and @RestCotroller in Spring MVC is that once you mark a class @RestController then every method is written a domain object instead of a view. You can see Bryan Hassen's Introduction to Spring MVC 4 to learn more about how to use the @RestController annotation in your Spring-based application.
 

@RestController vs @Controller Annotation in Spring MVC and REST




6. Another key difference between @RestController and @Controller is that you don't need to use @ResponseBody on every handler method once you annotate the class with @RestController as shown below:
with @RestControler
 

@RestController public class Book{  @RequestMapping(value={"/book"}) public Book getBook(){ //... return book; } }


without @RestController
 

@Controller public class Book{  @RequestMapping(value={"/book"}) @ResponseBody public Book getBook(){ //... return book; } }


You can see that if you use Spring MVC @Controller annotation to create a RESTful response you need to annotate each method with the @ResponseBody annotation, which is not required when you use @RestController. It not only makes your code more readable but also saves a couple of keystrokes for you.
 

Related blog:

Java training in chennai

創作者介紹
創作者 johndemorses 的頭像
johndemorses

johndemorses

johndemorses 發表在 痞客邦 留言(0) 人氣( 3 )