Difference Betwixt @Restcontroller Together With @Controller Musical Note Inwards Saltation Mvc Together With Rest

Advertisement

Masukkan script iklan 970x90px

Difference Betwixt @Restcontroller Together With @Controller Musical Note Inwards Saltation Mvc Together With Rest

Jumat, 02 Maret 2001

The @RestController notation inward Spring MVC is nada but a combination of @Controller as well as @ResponseBody annotation. It was added into Spring 4.0 to brand the evolution of RESTful Web Services inward Spring framework easier. If y'all are familiar alongside the REST spider web services y'all know that the telephone commutation deviation betwixt a spider web application as well as a REST API is that the response from a spider web application is mostly stance (HTML + CSS + JavaScript)  because they are intended for human viewers piece REST API precisely render information inward cast of JSON or XML because most of the REST clients are programs. This deviation is besides obvious inward the @Controller as well as @RestController annotation.

The chore of @Controller is to create a Map of model object as well as uncovering a stance but @RestController precisely render the object as well as object information is direct written into HTTP response equally JSON or XML.

This tin dismiss besides travel done alongside traditional @Controller as well as utilization @ResponseBody notation but since this is the default conduct of RESTful Web services, Spring introduced @RestController which combined the conduct of @Controller as well as @ResponseBody together.

In short, the next 2 code snippet are equal inward Spring MVC:

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

Obviously, everybody would similar to declare precisely i notation instead of two. Also, the @RestController is to a greater extent than obvious as well as telling than the previous two. Btw, if y'all are non familiar alongside Spring Core as well as Spring MVC, I advise y'all to start accept a await at the Spring Framework 5: Beginner to Guru course on Udemy. One of the up-to-date as well as comprehensive courses to acquire Spring.




What are @Controller as well as @RestController inward Spring?

In the Spring framework, H5N1 Controller is a cast which is responsible for preparing a model Map alongside information to travel displayed past times the stance equally good equally choosing the correct stance itself. It tin dismiss besides direct write into response current past times using @ResponseBody notation as well as consummate the request.

The conduct of writing direct into response current is real useful for responding calls to RESTful spider web services because their nosotros precisely render information instead of returning a stance equally explained inward my before shipping service most how Spring MVC plant internally.

If y'all conduct maintain developed RESTful Web services before Spring four similar inward Spring iii or Spring 3.1, y'all would conduct maintain been familiar alongside using a combination of @Controller as well as @ResponseBody to create a RESTful response. Spring guys accept cognizant of these issues as well as created @RestController.

Now, y'all don't hollo for to utilization @Controller as well as @RestponseBody annotation, instead y'all tin dismiss utilization @RestController to render the same functionality. In short, it is a convenience controller which combines the conduct of @Controler as well as @Response torso into one.

If y'all are interested inward developing RESTful spider web services alongside Spring as well as thus y'all tin dismiss farther bring together Eugen Paraschiv's REST alongside Spring Master class if y'all are to a greater extent than curious most learning the advanced techniques to prepare RESTFul Web Service inward Spring. One of the guided as well as code-focused agency to acquire Spring MVC.




Difference betwixt @RestController as well as @Controller inward Spring

Now that, y'all are familiar alongside both of these annotations, it's a expert fourth dimension to analyze approximately factual deviation betwixt @RestController as well as @Controler. This is a real of import concept, non precisely from the Interview dot of stance but besides from Spring Core  Developer Certification.

 If y'all are preparing for Spring certifications, y'all should familiar alongside such subtle differences. Additionally, y'all tin dismiss besides accept a await at gratuitous Spring tests to acquire an thought most examination format as well as degree of questions.

Anyway, let's acquire dorsum to the point, hither are approximately of import differences betwixt these 2 annotations.

1. The @Controller is a mutual notation which is used to grade a cast equally Spring MVC Controller piece @RestController is a exceptional controller used inward RESTFul spider web services as well as the equivalent of @Controller + @ResponseBody.

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

3. The @Controller notation indicates that the cast is a "Controller" similar a spider web controller piece @RestController notation indicates that the cast is a controller where @RequestMapping methods assume @ResponseBody semantics past times default i.e. servicing REST API.


4. The @Controller is a specialization of @Component notation piece @RestController is a specialization of @Controller annotation. It is genuinely a convenience controller annotated alongside @Controller as well as @ResponseBody equally shown below.

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

as well as hither is how the proclamation of @Controller looks like:

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


5. One of the key deviation betwixt @Controler as well as @RestCotroller inward Spring MVC is that i time y'all grade a cast @RestController as well as thus every method is written a domain object instead of a view. You tin dismiss encounter Bryan Hassen's RESTful response y'all hollo for to annotate each method alongside the @ResponseBody annotation, which is non required when y'all utilization @RestController. It non exclusively makes your code to a greater extent than readable but besides saves a twosome of keystrokes for you.


Here is a unproblematic HelloWorld illustration using @RestController as well as SpringBoot framework:

 notation inward Spring MVC is nada but a combination of  Difference betwixt @RestController as well as @Controller Annotation inward Spring MVC as well as REST

That's all most the difference betwixt @Controller as well as @RestController notation inward Spring MVC as well as REST. @RestController is nada but the shortcut to utilization both @Controller as well as @ResponseBody notation together.

Spring purposefully added this notation inward Spring four to brand the evolution of RESTful spider web services easier using the Spring framework. It tin dismiss direct convert the response to JSON or XML depending upon MIME type of request.

So, if y'all are creating a RESTful Web Services it's meliorate to utilization @RestController than combining @Controller to @ResponseBody.

Further Reading
courses)
  • 23 Spring MVC Interview questions for 2 to iii years experienced (list)
  • What is the utilization of DispatcherServlet inward Spring MVC? (answer)
  • 20+ Spring as well as REST Interview Questions (questions)
  • 3 ways to acquire Spring Framework meliorate (article
  • How to enable Spring safety inward Java application? (answer)
  • Does Spring certification attention inward Job as well as Career? (article)
  • Top v Spring Certification Mock Exams (list)
  • Difference between @Autowired as well as @Injection annotations inward Spring? (answer)
  • 5 Spring as well as Hibernate online courses for Java developers (list)
  • 10 Spring Annotation Java Developers Should Know (annotations)
  • Top v Spring Boot Features for Java Developers (features)

  • Thanks for reading this article. If y'all similar this article as well as thus delight portion alongside your friends as well as colleagues. If y'all conduct maintain whatsoever questions or feedback as well as thus delight driblet a comment as well as I'll seek to respond it equally presently equally possible.

    P. S. - If y'all desire to acquire to a greater extent than most developing RESTful Web Services using Spring as well as Spring Security framework, I advise y'all bring together Eugen Paraschiv's REST alongside Spring Certification class. Eugen has approximately expert real-world sense inward developing as well as securing RESTful spider web services inward Java as well as this cast is a expert chance to practice goodness from his immense experience.