使用基于@ResponseBody注释的MappingJackson2JsonView支持在Spring REST中返回JSON响应有什么好处? [英] What are the advantages of returning JSON response in Spring REST Using MappingJackson2JsonView Support over @ResponseBody Annotation?
问题描述
我正在浏览教程并发现我们可以在Spring REST中返回JSON响应使用 MappingJackson2JsonView
支持 @ResponseBody
注释。
I am going through this tutorial and found we can return JSON response in Spring REST Using MappingJackson2JsonView
Support over @ResponseBody
Annotation.
显然使用 @ResponseBody
注释非常简单,易于实现,使用 MappingJackson2JsonView
支持位复杂。这是我个人的意见,但我想知道在Spring REST中使用MappingJackson2JsonView支持在@ResponseBody Annotation上返回JSON响应的优点是什么?如果有的话?有人可以解释一下他们之间哪个更好,为什么? 何时使用MappingJackson2JsonView支持以及何时使用@ResponseBody Annotation 在Spring REST中返回JSON响应?
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?
推荐答案
<我认为,MappingJackson2JsonView是一个很好的实现。
I think, MappingJackson2JsonView is good implementation.
但是ResponseBody有一些优点。
在@ResponseBody的帮助下,您可以返回对象而不是属性
例如
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();
}
结果将是
{
name: John
}
Result will be { "name":"John" }
使用MappingJackson2JsonView,你必须在属性中返回对象,如
With MappingJackson2JsonView you must return object in property like
{
"student": {
"name":"John"
}
}
使用@ResponseBody你还可以返回内联对象延伸的结果
with @ResponseBody you can also return result with inline object extending
@ResponseBody getStudentInfo(){
return new Object(){
public String name="John"
};
}
但这不是很好的实施
一些好的实现MappingJackson2JsonView很好。但是对于更多功能,如返回对象或返回ResponseDto,ResponseBody更有用
Some for good implementation MappingJackson2JsonView is good. But for more functionality like as return object or return ResponseDto, ResponseBody is more usefull
这篇关于使用基于@ResponseBody注释的MappingJackson2JsonView支持在Spring REST中返回JSON响应有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!