Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of returning JSON response in Spring REST Using MappingJackson2JsonView Support over @ResponseBody Annotation?

I am going through this tutorial and found we can return JSON response in Spring REST Using MappingJackson2JsonView Support over @ResponseBody Annotation.

Apparently Using @ResponseBody Annotation is very simple and easy to implement where as Using MappingJackson2JsonView Support bit complex. Any way this is my personal opinion, but I want to know what are the advantages of returning JSON response in Spring REST Using MappingJackson2JsonView Support over @ResponseBody Annotation, if there is any? Can someone explain me which one is better between them and why? When to use MappingJackson2JsonView Support and when to use @ResponseBody Annotation for returning JSON response in Spring REST?

like image 800
Rajorshi Roy Avatar asked Jan 29 '16 20:01

Rajorshi Roy


People also ask

What is the purpose of the @ResponseBody annotation?

The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the HTTP response automatically.

What is @RequestBody and @ResponseBody annotation in Spring?

By using @RequestBody annotation you will get your values mapped with the model you created in your system for handling any specific call. While by using @ResponseBody you can send anything back to the place from where the request was generated. Both things will be mapped easily without writing any custom parser etc.

What is difference between @RequestBody and @ResponseBody?

@RequestBody and @ResponseBody annotations are used to bind the HTTP request/response body with a domain object in method parameter or return type. Behind the scenes, these annotation uses HTTP Message converters to convert the body of HTTP request/response to domain objects.


2 Answers

I think, MappingJackson2JsonView is good implementation.

But there is some advantages of ResponseBody. With Help of @ResponseBody you can return object not as property For example

class Student extends ResponseDTO{
   publci String name = "John"
}
...
@ResponseBody ResponseDTO  getStudentInfo(){
  return new Student();
}

Result will be { "name":"John" }

With MappingJackson2JsonView you must return object in property like

{
  "student": {
               "name":"John"
             }
}

with @ResponseBody you can also return result with inline object extending

 @ResponseBody getStudentInfo(){
  return new Object(){
     public String name="John"
 };
}

But this is not good implementation

Some for good implementation MappingJackson2JsonView is good. But for more functionality like as return object or return ResponseDto, ResponseBody is more usefull

like image 127
Taleh Ibrahimli Avatar answered Oct 05 '22 19:10

Taleh Ibrahimli


I will say that even it's a bit harder to implement, good implementation can be more flexible and powerful than @ResponseBody. For example:

Jackson2ObjectMapperBuilder provides a nice API to customize various Jackson settings while retaining Spring Framework provided default ones. It also allows to create ObjectMapper and XmlMapper instances based on the same configuration.

Both Jackson2ObjectMapperBuilder and Jackson2ObjectMapperFactoryBean define a better Jackson default configuration. For example, the DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES property set to false, in order to allow deserialization of JSON objects with unmapped properties.

But in general, it's somehow the matter of own opinion and experiences.

like image 44
m.aibin Avatar answered Oct 05 '22 19:10

m.aibin