使用@JsonView 排除(如@JsonIgnore)与播放框架默认的 json 编写器? [英] Use @JsonView to exclude (like @JsonIgnore) with play frameworks default json writer?

查看:11
本文介绍了使用@JsonView 排除(如@JsonIgnore)与播放框架默认的 json 编写器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来你不能混用@JsonIgnore 和@JsonView.我想默认隐藏一个字段,但在某些情况下会显示它.

It seems you can't mix @JsonIgnore and @JsonView. I want to hide a field by default, but show it in some cases.

基本上我有这个设置:-

Basically I've got this setup :-

class Parent extends Model {
  public Long id;
  public Child child;
}

class Child extends Model {
  public Long id;
  @JsonView(Full.class)
  public String secret;

  public static class Full {};
}

并且想要使用 play.libs.Json.toJson(parent) 来渲染而没有 child.secret,并且

And want to use play.libs.Json.toJson(parent) to render WITHOUT child.secret, and

    ObjectMapper objectMapper = new ObjectMapper();
    ObjectWriter w = objectMapper.writerWithView(Child.Full.class);
    return ok(w.writeValueAsString(child));

渲染 WITH child.secret.有没有办法做到这一点.即是否有任何方法可以将字段设置为默认忽略,但包含在特定的 JsonView 中?

to render WITH child.secret. Is there any way to do this. i.e. is there any way to set a field to ignore by default, but get included with a particular JsonView?

目前两个调用都包含秘密.

Currently BOTH calls include the secret.

谢谢!

推荐答案

一旦你有了一个对象映射器,你就可以像当前使用 play.libs.Json.toJson(parent),得到你想要的.

Once you have an object mapper, you can effectively use it in the same way as you are currently using play.libs.Json.toJson(parent), and get exactly what you want.

因此,无论何时您以前使用 play.libs.Json.toJson(parent),只需使用 new ObjectMapper().writeValueAsString() 就不会得到你的秘密.

So whenever you were previously using play.libs.Json.toJson(parent), just use new ObjectMapper().writeValueAsString() and you won't get your secret.

这篇关于使用@JsonView 排除(如@JsonIgnore)与播放框架默认的 json 编写器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆