关于@JsonView的使用心得

一、一般情况下的使用

1、声明View接口

在你想要声明多个视图的类中定义相应视图的view接口,视图之间可以继承,子视图会包含父视图的内容

2、在属性的get方法上指定视图

如果你想要某一个属性在某一种视图中出现,那么只需在对应属性的get方法上使用@JsonView注解声明其所在视图即可。本文在Notification类中声明三个视图,如下:

public class Notification implements Serializable{private static final long serialVersionUID = -2200572370385250074L;public interface AlertView{}public interface CNView extends AlertView{}public interface MNView extends CNView{}private BigInteger id;private Integer type;private String holder;private String time;private String reason;private boolean isRead;private String protrait;private String detail;private String address;private String company;private String roomId;private String latitude;private String longitude;private String image;private List<String> devices;private String instruction;private Integer capacity;private List<String> roomImages;private List<Attendees> attendees;private Booking booking;public Notification() {}@JsonView(MNView.class)public String getReason() {return reason;}public void setReason(String reason) {this.reason = reason;}@JsonView(AlertView.class)public BigInteger getId() {return id;}public void setId(BigInteger id) {this.id = id;}@JsonView(AlertView.class)public Integer getType() {return type;}public void setType(Integer type) {this.type = type;}@JsonView(CNView.class)public String getHolder() {return holder;}public void setHolder(String holder) {this.holder = holder;}@JsonView(CNView.class)public String getTime() {return time;}public void setTime(String time) {this.time = time;}@JsonView(AlertView.class)public boolean isRead() {return isRead;}public void setRead(boolean read) {isRead = read;}@JsonView(CNView.class)public String getProtrait() {return protrait;}public void setProtrait(String protrait) {this.protrait = protrait;}@JsonView(CNView.class)public String getDetail() {return detail;}public void setDetail(String detail) {this.detail = detail;}@JsonView(CNView.class)public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@JsonView(CNView.class)public String getCompany() {return company;}public void setCompany(String company) {this.company = company;}@JsonView(CNView.class)public String getRoomId() {return roomId;}public void setRoomId(String roomId) {this.roomId = roomId;}@JsonView(CNView.class)public String getLatitude() {return latitude;}public void setLatitude(String latitude) {this.latitude = latitude;}@JsonView(CNView.class)public String getLongitude() {return longitude;}public void setLongitude(String longitude) {this.longitude = longitude;}@JsonView(CNView.class)public String getInstruction() {return instruction;}public void setInstruction(String instruction) {this.instruction = instruction;}@JsonView(CNView.class)public Integer getCapacity() {return capacity;}public void setCapacity(Integer capacity) {this.capacity = capacity;}@JsonView(Notification.CNView.class)public Booking getBooking() {return booking;}public void setBooking(Booking booking) {this.booking = booking;}@JsonView(AlertView.class)public String getImage() {return image;}public void setImage(String image) {this.image = image;}@JsonView(CNView.class)public List<String> getDevices() {return devices;}public void setDevices(List<String> devices) {this.devices = devices;}@JsonView(CNView.class)public List<String> getRoomImages() {return roomImages;}public void setRoomImages(List<String> roomImages) {this.roomImages = roomImages;}@JsonView(Notification.CNView.class)public List<Attendees> getAttendees() {return attendees;}public void setAttendees(List<Attendees> attendees) {this.attendees = attendees;}
}

3、在Controller方法上指定视图

在完成上面两步后,只需在Controller方法上使用@JsonView注解声明需要向前端返回的视图即可,如果不声明视图则会忽略类中的注解,返回所有的字段。如下:

 @JsonView(Notification.AlertView.class)@RequestMapping(value = "/testSendN",method = RequestMethod.GET)public Notification testSendNotification(){List<String> roomImages=new ArrayList<String>();String roomImage=new String("https://www.jsjzx.top/Volunteer/Images/20.1.jpg");roomImages.add(roomImage);String protrait=new String("https://www.jsjzx.top/Volunteer/Images/b.jpg");List<String> devices=new ArrayList<String>();devices.add("麦克风");devices.add("投影仪");String routine=new String("https://www.jsjzx.top/Volunteer/Images/tt.png");String goods=new String("https://www.jsjzx.top/Volunteer/Images/2a.jpg");Booking booking=new Booking(BigInteger.valueOf(2),goods,"柠檬水",8,5);List<Attendees> attendees=new ArrayList<Attendees>();Attendees attendee1=new Attendees();attendee1.setName("陆凝丹");attendee1.setUri("https://www.jsjzx.top/Volunteer/Images/a.jpg");Attendees attendee2=new Attendees();attendee2.setUri("https://www.jsjzx.top/Volunteer/Images/b.jpg");attendee2.setName("付初露");attendees.add(attendee1);attendees.add(attendee2);Notification notification=new Notification();notification.setId(BigInteger.valueOf(2));notification.setAddress("浙江省杭州市拱墅区祥园路99号");notification.setCapacity(20);notification.setCompany("运河广告产业大厦");notification.setDetail("更简洁");notification.setHolder("陆凝丹");notification.setInstruction("不可损坏设备");notification.setLatitude("30.3368799267");notification.setLongitude("120.1175165193");notification.setRead(false);notification.setReason("由于设备故障导致会议室暂时无法使用,很抱歉给您造成困扰,现已经为您更换了会议室,以下为会议室详细信息");notification.setRoomId("D05");notification.setTime("2019年6月14日9:00");notification.setType(2);notification.setRoomImages(roomImages);notification.setProtrait(protrait);notification.setDevices(devices);notification.setImage(routine);notification.setBooking(booking);notification.setAttendees(attendees);return notification;}

4、查看是否完成预期效果

@JsonView(Notification.AlertView.class)的结果如下

@JsonView(Notification.CNView.class)的结果如下:

@JsonView(Notification.MNView.class)的结果如下:
比CNView多了reason字段

二、一些隐蔽的注意事项

  • Controller 根据声明的视图来过滤字段,在过滤时如果在类中有一些属性声明了视图,有一些没有,则没有声明视图的属性会被过滤掉,不会返回给前端
  • 如果将声明视图的类作为另外一个类的属性,则在controller方法上声明的视图将失效,返回的数据为空。举个例子:如果重新声明一个CommonResult类,Notification作为它的一个属性,如下:
` @JsonView(Notification.MNView.class)@RequestMapping(value = "/testSendN",method = RequestMethod.GET)public CommonResult testSendNotification(){CommonResult result=new CommonResult();Notification notification=appointmentService.sendNotification(BigInteger.valueOf(2));result.setData(notification);return result;}`

则此时返回给前端的数据为空:

原因是在CommonResult中没有找到@JsonView所声明的视图,所以全部被过滤掉了,所以解决方法是在CommonResult中也声明同样的视图,但这样就增加了CommonResult与视图所在类的耦合。但在有些时候我们希望一些类是为整个项目服务的,所以这样的方法就不适用了,除非你声明一个全局的View接口,项目中其他的视图都继承这个全局的view

  • 在声明视图的类中如果有其他自定义的类为属性且该属性类没有声明相同的视图,则改属性会被过滤掉,举个例子,在上述Notification类中的booking属性及attendees属性均为自定义的类,且booking属性上声明了CNView视图,但如果在Booking类中各项属性没有声明同样的视图,则booking属性会被过滤掉,如下:
    可以看到booking属性是空的。
    解决方法是在Booking类中加入相同的视图注解,如下:
public class Booking implements Serializable {private static final long serialVersionUID = 1290811415389745003L;private BigInteger id;private String image;private String title;private double price;private Integer count;public Booking() {}public Booking(BigInteger id, String image, String title, double price, Integer count) {this.id = id;this.image = image;this.title = title;this.price = price;this.count = count;}@JsonView(Notification.CNView.class)public BigInteger getId() {return id;}public void setId(BigInteger id) {this.id = id;}@JsonView(Notification.CNView.class)public String getImage() {return image;}public void setImage(String image) {this.image = image;}@JsonView(Notification.CNView.class)public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}@JsonView(Notification.CNView.class)public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@JsonView(Notification.CNView.class)public Integer getCount() {return count;}public void setCount(Integer count) {this.count = count;}
}

关于@JsonView的使用心得及一些隐蔽的注意事项相关推荐

  1. 新手应该如何有效地学习.net

    前言     对于新手来说,学习.NET编程是一件很痛苦的事情,这倒不是因为学习.NET是一件很难的事情,而是.NET是一个庞大的学习体系,对于新手来会感觉无从下手,从而造成永远都无法入门,看到别人成 ...

  2. 高手教程——MF的技巧

    话说知己知彼,百战百胜,如何对付和利用魔兽中的第三方--中立怪物,也是一个需要player们去关注的战斗层面,因为中立怪物们不仅能够给我们提供经验和宝物,还有一部分金钱.怎样杀怪才能达到最佳效果呢?就 ...

  3. 分享一些面试中的经验和心得

    " 本文作者在2019年实习和秋招中面了10多家公司,只吃过一次拒信,拿到的offer中不乏一些竞争非常激烈的外企与国内大厂的ssp,而且开发/算法的offer都有.值得一提的是作者本科并非 ...

  4. Notepad++ 使用心得

    作为一个菜鸟程序员,一直很喜欢windows平台下的notepad++,轻巧,可定制性强,感觉很好用.下面讲一下我使用notepad++时的一些心得. 快捷键 这个是比较基础的,我们可以在设置-> ...

  5. 读LEO《程序员羊皮卷》心得笔记

    完成一个满意的项目丰富自己 安排一次真正的实习,了解社会 学校渠道早动手,网上渠道全知道,朋友渠道要用到. 永远记住,工作中没有什么事是理所当然的,总有人fuck in your ass 我们的实习应 ...

  6. 通信工程项目实施心得体会

    前言 图文 内容 经验 结尾 总结 前言 在信息时代,通信技术的进步和发展日新月异,为了适应这一发展趋势,凭借着对通信技术的敏感与专业知识,我积极投身于通信工程项目的实施工作.在项目实施过程中,我对通 ...

  7. 庄子心得08:谈笑论生死

    [画外音]生与死,是人生起始的两个端点,而人生就像一条不归路,当你走到终点时,才会想起途中的遗撼.庄子之所以能够笑谈生死,是因为他悟出了生死的真谛,那就是生和死,不过是一个形态的变化.于丹教授认为,只 ...

  8. 装修的200条小常识,有心得的来讨论下了

    装修的200条小常识,有心得的来讨论下了 1.鞋柜的隔板不要做到头,留一点空间好让鞋子的灰能漏到最底层,水槽和燃气灶上方装灯.定卫生间地漏的位置时一定要先想好,量好尺寸.地漏最好位于砖的一边,如果在砖 ...

  9. 通过rootkit隐蔽行踪 Linux提权

    每天必问必答: ***1.每天在学什么? ***2.学了有什么用? ***3.工作中如何用? 心得:一定要把实验出错的地方.原因.解决方案都总结到笔记上.笔记一定要自己写的详细些! 服务端:xuego ...

  10. 做了 3 年企业级 SaaS,我收获的 10 点心得

    关于中国企业级服务的总结不少,本土派和海外派都有出色的文章出来,VC 和创业者站在各自角度也有很多不错的总结.本文基于 Ping++ 近三年的创业历程而来,有弯路,有教训,有醒悟,也有心得.盛景 B2 ...

最新文章

  1. a.cmd 文件里的内容
  2. 【转载】IT新曙光——“遇事不决,量子力学” 的问与答
  3. MySQL UPDATE 语句一个“经典”的坑
  4. 基于Apache POI 从xlsx读出数据
  5. 关于mysql中外键关联的一些个人理解
  6. MFC创建模式对话框与非模式对话框
  7. 02、MySQL—数据库基本操作
  8. windows下consul安装启动
  9. swift - scrollview 判断左右移动, 以及上下两个view联动
  10. 从中关村的小小柜台,但目前市值千亿元的公司
  11. Activity常用设置
  12. python json库函数_Python JSON
  13. Flutter 学习汇总
  14. iOS如何优雅的处理“回调地狱Callback hell”(一)——使用PromiseKit
  15. python使用 Captcha 模块来生成验证码图片
  16. nlp自然语言处理_nlp满足可持续投资
  17. FAST角点检测算法(二)- 非极大值抑制筛选fast特征点
  18. 计算机初学者需要知道的一些DOS命令
  19. 《算法0基础100讲》(第7讲)素数判定——866.回文素数
  20. 《华尔街》观后笔记8——明暗创新

热门文章

  1. 大学生创新创业 /互联网+ 大赛 商业计划书目录(模板)
  2. Jsp 购物车(oracle数据库)之初级版
  3. C语言SM4算法实现(基于GMSSL)
  4. 物联网定位技术超全解析
  5. 深度学习:智能时代的核心驱动力量
  6. python怎么批量下载图片_批量下载网页图片(python)
  7. 黑群晖linux删除文件夹命令,不拆机直接修改黑群晖的SN和MAC
  8. 如何使用vs进行代码比较
  9. 2010-6-15 Linux 学习笔记
  10. Web视频播放总结,avi、swf、兼容IE6/7/8