Spring MVC @JsonView使用详解

原文出处:http://blog.csdn.net/gefangshuai/article/details/50328361

Spring 提供了对Jackson 非常好的支持,这里着重说一下@JsonView的用法

JSON Views 基本用法

@JsonView可以过滤序列化对象的字段属性,可以使你有选择的序列化对象。下面看一个例子: 
首先我们定义一个View类,里面包含我们对“要序列化的字段”的定义,我们暂时归为Summary(摘要)。属于Summary的字段都是我们要序列化的字段。

这里也可以将View类理解为一组“标识”,而Summary就是其中的一个“标识”。

public class View {
   interface Summary {}
}
  • 1
  • 2
  • 3

View类的定义很简单。

然后我们定义我们的Bean类:User

public class User { 
  @JsonView(View.Summary.class) 
  private Long id; 
  @JsonView(View.Summary.class) 
  private String firstname; 
  @JsonView(View.Summary.class) 
  private String lastname; 
  private String email; 
  private String address; 
  private String postalCode; 
  private String city; 
  private String country;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

可以看到,@JsonView使用很简单,加在属性上,指定一个“标识”就好了。上面例子我们将Summary指定给了idfirstnamelastname三个属性,这样,当我们使用@JsonView序列化User对象的时候,就只会序列化这三个属性,可以隐藏一些不想序列化的字段属性。 
SpringMVC根据@JsonView序列化的方法如下:

@RestController
public class UserRestController{
  @Autowired 
  private UserService userService;

  @RequestMapping("/user")
  @JsonView(View.Summary.class) 
  public List<User> getUsers(){
    return userService.listUsers();
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在Controller中指定JsonVIew即可。 
当我们访问/user时,会得到如下结果:

[
  { "id" : 1, "firstname" : "Brian", "lastname" : "Clozel" },
  { "id" : 2, "firstname" : "Stéphane", "lastname" : "Nicoll" },
  { "id" : 3, "firstname" : "Rossen", "lastname" : "Stoyanchev" }
]
  • 1
  • 2
  • 3
  • 4
  • 5

用法是不是很简单?

JSON Views 继承用法

JsonView 同时还支持继承 
比如我们需要将User分两个规则进行序列化,一个是“基本资料”,即上面那三个属性的规则;另一个是“详细资料”,除了包含“基本资料”中的三个属性外,另外还要包含几个详细资料emailaddress

我们在View中再加一个“标识”SummaryWithDetail,定义如下:

扫描二维码关注公众号,回复: 343345 查看本文章
public class View {
  interface Summary {}
  interface SummaryWithDetail extends Summary{}
}
  • 1
  • 2
  • 3
  • 4

这里使用了继承,SummaryWithDetail继承了Summary,也就是说,当我们以SummaryWithDetail的方式进行对象序列化时,出来的属性同时也包含Summary指定的属性。我们在User中指定SummaryWithDetail:

public class User { 
  @JsonView(View.Summary.class) 
  private Long id; 
  @JsonView(View.Summary.class) 
  private String firstname; 
  @JsonView(View.Summary.class) 
  private String lastname; 

  @JsonView(View.SummaryWithDetail .class) 
  private String email; 
  @JsonView(View.SummaryWithDetail .class) 
  private String address; 

  private String postalCode; 
  private String city; 
  private String country;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

可以看到,我们对emailaddress指定了SummaryWithDetail标识。

增加Controller方法,测试:

@RestController
public class UserRestController{
  @Autowired 
  private UserService userService;

  @RequestMapping("/user")
  @JsonView(View.Summary.class) 
  public List<User> getUsers(){
    return userService.listUsers();
  }

  @RequestMapping("/user-with-detail")
  @JsonView(View.Summary.class) 
  public List<User> getUsersWithDetail(){
    return userService.listUsers();
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这样,当我们访问/user-with-detail时,会得到如下结果:

[
  { 
    "id" : 1, 
    "firstname" : "Brian", 
    "lastname" : "Clozel", 
    "email": "[email protected]", 
    "address": "beijing China" 
  },
  { 
    "id" : 2, 
    "firstname" : "Stéphane", 
    "lastname" : "Nicoll", 
    "email": "[email protected]", 
    "address": "beijing China" 
   },
  { 
    "id" : 3, 
    "firstname" : "Rossen", 
    "lastname" : "Stoyanchev" , 
    "email": "[email protected]", 
    "address": "beijing China" 
  }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

其实要想实现对属性的排除过滤,Jackson还有一种更简单的方法:@JsonFilter。 
官方文档地址:http://wiki.fasterxml.com/JacksonFeatureJsonFilter

猜你喜欢

转载自913.iteye.com/blog/2385027