Difference between @RestController and @Controller
The @RestController annotation in Spring MVC is nothing but a combination of the @Controller and the @ResponseBody annotation. It was added into Spring 4.0 to make the development of RESTful Web Services in Spring framework easier. If you are familiar with the REST web services you know that the fundamental difference between a web application and a REST API is that the response from a web application is a generally view of HTML + CSS + JavaScript while REST API just return data in form of JSON or XML. This difference is also obvious in the @Controller and the @RestController annotation. The job of the @Controller is to create a Map of model object and find a view but the @RestController simply returns the object and object data is directly written into HTTP response as JSON or XML.
This can also be done with the traditional @Controller and the use of the @ResponseBody annotation but since this is the default behavior of RESTful Web services, Spring introduced @RestController which combined the behavior of @Controller and @ResponseBody together.
This can also be done with the traditional @Controller and the use of the @ResponseBody annotation but since this is the default behavior of RESTful Web services, Spring introduced @RestController which combined the behavior of @Controller and @ResponseBody together.
1@Controller
2@ResponseBody
3public class MVCController {
4 .. your logic
5}
1@RestController
2public class RestFulController {
3 .... your logic
4}
What are @Controller and @RestController in Spring?
In Spring framework, a
is a class which is responsible for preparing a model Map with data to be displayed by the view as well as choosing the right view itself. It can also directly write into response stream by using annotation and complete the request.
The behavior of writing directly into response stream is very useful for responding calls to RESTful web services because there we just return data instead of returning a view.
If you have developed RESTful Web services before Spring 4 e.g. in Spring 3 or Spring 3.1, you would have been familiar by using a combination of the
and the to create a RESTful response. Spring guys take cognizant of this issues and created the .
Now, you don’t need to use the
and the annotation. Instead you can use the to provide the same functionality. In short, it is a convenience controller which combines the behavior of the and the body into one.Difference between @RestController and @Controller in Spring
Now that, you are familiar with both of these annotations, it’s a good time to analyze some factual difference between the
and the .
Anyway, let’s get back to the point, here are some important differences between these two annotations.
- The RESTFul web services and the equivalent of . is a common annotation which is used to mark a class as Spring MVC Controller while the is a special controller used in
- The is relatively new, added only on Spring 4.0 but is an old annotation, exists since Spring started supporting annotation, and officially it was added on Spring 2.5 version.
- The annotation indicates that the class is a “Controller” e.g. a web controller while the annotation indicates that the class is a controller where methods assume semantics by default i.e. servicing REST API.
- The 123456
@Target
(value=TYPE)
@Retention
(value=RUNTIME)
@Documented
@Controller
@ResponseBody
public
@interface
RestController
and here is how the declaration oflooks like:12345@Target
(value=TYPE)
@Retention
(value=RUNTIME)
@Documented
@Component
public
@interface
Controller
is a specialization of annotation while is a specialization of annotation. It is actually a convenience controller annotated with and as shown below. - One of the key difference between and in Spring MVC is that once you mark a class as then every method is written a domain object instead of a view.
- Another key difference between with @RestControler:123456789
@RestController
public
class
Book{
@RequestMapping
(value={
"/book"
})
public
Book getBook(){
//...
return
book;
}
}
without @RestController:01020304050607080910@Controller
public
class
Book{
@RequestMapping
(value={
"/book"
})
@ResponseBody
public
Book getBook(){
//...
return
book;
}
}
and is that you don’t need to use on every handler method once you annotate the class with as shown below:
You can see that if you use Spring MVC RESTful response you need to annotate each method with the annotation, which is not required when you use . It not only makes your code more readable but also saves a couple of key strokes for you.
annotation to create a
Here is a simple HelloWorld example using
and SpringBoot framework:
That’s all about the difference between
and annotation in Spring MVC and REST. is nothing but the shortcut to use both and annotation together.
Spring purposefully added this annotation in Spring 4 to make the development of RESTful web services easier using Spring framework. It can directly convert the response to JSON or XML depending upon MIME type of request.
So, if you are creating a RESTful Web Services it’s better to use
than combining the to .
Comments
Post a Comment