java 可以在每次调用的基础上设置属性 @JSONIgnore 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11438972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:08:25  来源:igfitidea点击:

Can the property @JSONIgnore be set on a per call basis?

javajsonHymanson

提问by EJay

I am working on a site that uses promo codes to potentially provide users free memberships to the site. The level that a member signs up at is an attribute of a promo code object and stored in the database as an int, either 1 or 2. However, the only time the member level attribute is relevant to the UI of the site is for a landing page associated with a specific promo code. So for that case, I don't want the member level to be ignored by JSON. After a user returns to the site and has a promo code object associated with their account, we no longer care about their member level so I would like the member level attribute to be ignored by JSON.

我正在使用促销代码为用户提供该网站的免费会员资格的网站上工作。会员注册的级别是促销代码对象的一个​​属性,并作为整数(1 或 2)存储在数据库中。但是,会员级别属性与站点 UI 相关的唯一时间是与特定促销代码相关联的登录页面。因此,对于这种情况,我不希望 JSON 忽略成员级别。在用户返回站点并拥有与其帐户关联的促销代码对象后,我们不再关心他们的会员级别,因此我希望 JSON 忽略会员级别属性。

So my question is this, is it possible to set an object's attribute to @JSONIgnoreon a per access basis, or can an attribute only be ignored or not ignored? In other words is there a method like object.getAttribute().setAttributeJSONIgnore(true | false)?

所以我的问题是,是否可以@JSONIgnore在每次访问的基础上将对象的属性设置为,或者只能忽略或不忽略属性?换句话说,有没有像这样的方法object.getAttribute().setAttributeJSONIgnore(true | false)

采纳答案by StaxMan

No. Annotation-based approaches are static, since although you could use custom AnnotationIntrospector, resulting JsonSerializerinstances are reused since their construction is costly.

不可以。基于注解的方法是静态的,因为虽然您可以使用 custom AnnotationIntrospector,但结果JsonSerializer实例会被重用,因为它们的构建成本很高。

But there are ways to filter out properties, check out "Filtering Properties" article, for example.

但是有一些方法可以过滤掉属性,例如查看“过滤属性”文章。

回答by Walt Moorhouse

I've done something similar with JSONViews (http://wiki.fasterxml.com/HymansonJsonViews).

我已经用 JSONViews ( http://wiki.fasterxml.com/HymansonJsonViews)做了类似的事情。

You'd create a Base View and then extend it with a PromoPage View. In the object you're serializing, you'd add the annotation to each property. So the ones you want to appear every time would be

您将创建一个基本视图,然后使用 PromoPage 视图扩展它。在您序列化的对象中,您需要为每个属性添加注释。所以你想每次出现的都是

@JsonView(Views.Base.class) 
String name;

The properties you only wanted to show on those certain Promo Pages would be something like

您只想在某些促销页面上显示的属性类似于

@JsonView(Views.PromoPage.class) 
Integer memberLevel;

Then if you have different JAX-RS methods for getting the promopages than the other landing page, you can annotate the methods with the appropriate View like so

然后,如果您使用与其他登录页面不同的 JAX-RS 方法来获取促销页面,则可以使用适当的视图对这些方法进行注释,如下所示

  @JsonView(Views.Base.class)
  @GET
  @Produces(MediaType.APPLICATION_JSON )
  public Object getNormalLandingPageInfo() {
    ...
  }

  @JsonView(Views.PromoPage.class)
  @GET
  @Produces(MediaType.APPLICATION_JSON )
  public Object getPromoLandingPageInfo() {
    ...
  }

If you don't, in your logic, you can do whatever you want to determine when to show the PromoPage View, then do something like this

如果你不这样做,在你的逻辑中,你可以做任何你想做的事情来决定何时显示 PromoPage 视图,然后做这样的事情

if (showPromoPage)
    objectMapper.writeValueUsingView(out, object, Views.PromoPage.class);
else
    objectMapper.writeValueUsingView(out, object, Views.Base.class);