简体   繁体   English

我如何有条件地从 json 响应中隐藏某些属性

[英]How can i hide some attribute conditionally from json response

how can write condition 'if' type is no details other two fields are hide in json response.如何编写条件“如果”类型没有详细信息,其他两个字段隐藏在 json 响应中。 if possible i want to do it in pojo or bean?如果可能的话,我想用 pojo 或 bean 做吗?

Using spring boot使用 spring 开机

  • spring data rest and hal. spring 数据 rest 和 hal。
  • pojo波乔
  • MongoDB Repository MongoDB 存储库

I want to show accountNo and Accountdetails if type="details"如果 type="details" 我想显示 accountNo 和 Accountdetails

{
   "Name":"Json",
   "lastName":"Amazon",
   "type":"Details",
   "accountNo":"12123",
   "AccountdetailsDetails":[ some details]
}

If type="noDetails" just show mentioned response.如果 type="noDetails" 只显示提到的响应。

{
   "Name":"Json", 
   "lastName":"Amazon",
   "type":"NoDetails"
}

I guess you need @JsonFilter .我猜你需要@JsonFilter You can use this annotation to exclude some properties in your entity response.您可以使用此注释排除实体响应中的某些属性。

What you need to do is Add this annotation with unique name in your entity file.您需要做的是在实体文件中添加具有唯一名称的注释。 Then serialize this entity values using filter class SimpleFilterProvider .然后使用过滤器 class SimpleFilterProvider序列化此实体值。

Take a look on https://www.logicbig.com/tutorials/misc/jackson/json-filter-annotation.html看看https://www.logicbig.com/tutorials/misc/jackson/json-filter-annotation.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM