Tuesday, April 23, 2024
HomeJavaDistinction between @RestController and @Controller Annotation in Spring MVC and REST

Distinction between @RestController and @Controller Annotation in Spring MVC and REST


The @RestController annotation in Spring MVC is nothing however a mixture of @Controller and @ResponseBody annotation. It was added into Spring 4.0 to make the event of RESTful Net Companies in Spring framework simpler. If you’re accustomed to the REST net companies you already know that the basic distinction between an internet software and a REST API is that the response from an internet software is mostly view (HTML + CSS + JavaScript)  as a result of they’re meant for human viewers whereas REST API simply returns information in type of JSON or XML as a result of a lot of the REST shoppers are applications. This distinction can also be apparent within the @Controller and @RestController annotation.

The job of @Controller is to create a Map of the mannequin object and discover a view however @RestController merely returns the article and object information is straight written into HTTP response as JSON or XML.

This will also be achieved with conventional @Controller and use @ResponseBody annotation however since that is the default habits of RESTful Net companies, Spring launched @RestController which mixed the habits of @Controller and @ResponseBody collectively.

Briefly, the next two code snippet are equal in Spring MVC:

@Controller
@ResponseBody
public class MVCController { 
   .. your logic
}

@RestController
public class RestFulController { 
  .... your logic
}

Clearly, everyone wish to declare only one annotation as a substitute of two. Additionally, the @RestController is extra apparent and telling than the earlier two. Btw, if you’re not accustomed to Spring Core and Spring MVC, I recommend you first check out the Spring Framework: Newbie to Guru course on Udemy. One of many up-to-date and complete programs to study Spring.

What are @Controller and @RestController in Spring?

Within the Spring framework, A Controller is a category that’s accountable for getting ready a mannequin Map with information to be displayed by the view in addition to selecting the best view itself. It may well additionally straight write into the response stream by utilizing @ResponseBody annotation and full the request.

The habits of writing straight into response stream could be very helpful for responding calls to RESTful net companies as a result of their we simply return information as a substitute of returning a view as defined in my earlier publish about how Spring MVC works internally.

If in case you have developed RESTful Net companies earlier than Spring 4 like in Spring 3 or Spring 3.1, you’d have been accustomed to utilizing a mixture of @Controller and @ResponseBody to create a RESTful response. Spring guys take cognizant of those points and created @RestController.

Now, you need not use @Controller and @RestponseBody annotation, as a substitute you need to use @RestController to supply the identical performance. Briefly, it’s a comfort controller that mixes the habits of @Controler and @Response physique into one.

If you’re occupied with growing RESTful net companies with Spring then you possibly can additional be a part of Eugen Paraschiv’s REST with Spring Grasp class if you’re extra interested by studying the superior strategies to develop RESTFul Net Service in Spring. One of many guided and code-focused option to study Spring MVC.

Difference between @RestController and @Controller in Spring

Distinction between @RestController and @Controller in Spring

Now that, you’re accustomed to each of those annotations, it is a good time to investigate some factual variations between @RestController and @Controler. This can be a essential idea, not simply from the Interview perspective but in addition from Spring Core  Developer Certification.

 If you’re getting ready for Spring certifications, it’s best to accustomed to such refined variations. Moreover, you too can check out free Spring checks to get an concept concerning the examination format and stage of questions.

Anyway, let’s get again to the purpose, listed here are some essential variations between these two annotations.

1. The @Controller is a typical annotation that’s used to mark a category as Spring MVC Controller whereas @RestController is a particular controller utilized in RESTFul net companies and the equal of @Controller + @ResponseBody.

2. The @RestController is comparatively new, added solely on Spring 4.0 however @Controller is an outdated annotation, exists since Spring began supporting annotation, formally it was added on Spring 2.5 model. You’ll be able to study extra about @RestController and different Spring 4 annotations on Grasp RESTful Net Companies with Spring Boot course on Udemy. Spring boot actually makes it straightforward to develop REST APIs with spring.

3. The @Controller annotation signifies that the category is a “Controller” like an internet controller whereas @RestController annotation signifies that the category is a controller the place @RequestMapping strategies assume @ResponseBody semantics by default i.e. servicing REST API.

4. The @Controller is a specialization of @Element annotation whereas @RestController is a specialization of @Controller annotation. It’s truly a comfort controller annotated with @Controller and @ResponseBody as proven under.

@Goal(worth=TYPE)
@Retention(worth=RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController

and right here is how the declaration of @Controller seems to be like:

@Goal(worth=TYPE)
@Retention(worth=RUNTIME)
@Documented
@Element
public @interface Controller

5. One of many key variations between @Controler and @RestCotroller in Spring MVC is that when you mark a category @RestController then each methodology is written a website object as a substitute of a view. You’ll be able to see Bryan Hassen’s Introduction to Spring MVC 4 to study extra about the right way to use the @RestController annotation in your Spring-based software.

@RestController vs @Controller Annotation in Spring MVC and REST

6. One other key distinction between @RestController and @Controller is that you just need not use @ResponseBody on each handler methodology when you annotate the category with @RestController as proven under:

with @RestControler

@RestController
public class Guide{

@RequestMapping(worth={"/guide"})
public Guide getBook(){
//...
return guide;
}
}

with out @RestController

@Controller
public class Guide{

@RequestMapping(worth={"/guide"})
@ResponseBody
public Guide getBook(){
//...
return guide;
}
}

You’ll be able to see that when you use Spring MVC @Controller annotation to create a RESTful response you could annotate every methodology with the @ResponseBody annotation, which isn’t required if you use @RestController. It not solely makes your code extra readable but in addition saves a few keystrokes for you.

Right here is an easy HelloWorld instance utilizing @RestController and Spring Boot framework:

Difference between @RestController and @Controller Annotation in Spring MVC and REST
That is all concerning the distinction between @Controller and @RestController annotation in Spring MVC and REST. @RestController is nothing however the shortcut to make use of each @Controller and @ResponseBody annotation collectively. In different phrases, @Controller is used to controller which might settle for and return HTML whereas @RestController annotation can be utilized to return JSON response. 

Spring purposefully added this annotation in Spring 4 to make the event of RESTful net companies simpler utilizing the Spring framework. It may well straight convert the response to JSON or XML relying upon the MIME kind of request.

So, if you’re making a RESTful Net Companies it is higher to make use of @RestController than combining @Controller to @ResponseBody.

Different Spring associated articles chances are you’ll wish to discover this weblog

  • 5 Free Spring and Spring Boot Programs to study on-line (programs)
  • 23 Spring MVC Interview questions for two to three years skilled (checklist)
  • What’s the usage of DispatcherServlet in Spring MVC? (reply)
  • 20+ Spring and REST Interview Questions (questions)
  • 3 methods to study Spring Framework higher (article
  • 25 Spring Safety Interview Questions (spring safety questions)
  • The way to allow Spring safety in Java software? (reply)
  • 13 Spring Actuator interview questions (spring actuator questions)
  • Does Spring certification assist in Job and Profession? (article)
  • High 5 Spring Certification Mock Exams (checklist)
  • Distinction between @Autowired and @Injection annotations in Spring? (reply)
  • 5 Spring and Hibernate on-line programs for Java builders (checklist)
  • 20 Spring Boot Testing Interview questions (Spring boot questions)
  • 10 Spring Annotation Java Builders Ought to Know (annotations)
  • High 5 Spring Boot Options for Java Builders (options)

Thanks for studying this text. In the event you like this text then please share it with your folks and colleagues. If in case you have any questions or suggestions then please drop a remark and I am going to attempt to reply it as quickly as potential.
P. S. – If you wish to study extra about growing RESTful Net Companies utilizing Spring and Spring Safety framework, I recommend you be a part of Eugen Paraschiv’s REST with Spring certification class. Eugen has some good real-world expertise in growing and securing RESTful net companies in Java and this class is an efficient alternative to learn from his immense expertise.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments