忽略Json忽略Elasticsearch [英] Ignore JsonIgnore in Elasticsearch

查看:145
本文介绍了忽略Json忽略Elasticsearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Spring-boot,关系数据库和Elasticsearch的应用程序。



我在代码的2个不同位置使用JSON序列化:




  • 响应REST API。

  • 当代码与Elasticsearch交互时。



Elasticsearch中需要一些属性,但我想向应用程序用户隐藏(例如,来自关系数据库的内部ID)。 p>

以下是实体的示例:

  @Document 
公共类MyElasticsearchEntity {

@Id
private Long id; //我想将其隐藏给用户。
私有字符串名称;
私有字符串描述;
}

问题:当对象在Elasticsearch中持续存在时,它将序列化为JSON。因此,当序列化到Elasticsearch时,具有 @JsonIgnore 的字段将被忽略。



到目前为止,我发现了2个不令人满意的解决方案:



解决方案1 ​​:像这样使用 @JsonProperty

  @Id 
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
私有Long id;

id 是用Elasticsearch编写的,在JSON响应中无效:

  {
id:null,
name: abc,
描述:null
}

应用程序用户仍然看到此属性存在。



解决方案2 :对对象映射器进行裁剪以忽略空值



Spring-boot有一个内置选项:

  spring.jackson.serialization-inclusion = NON_NULL 

问题:它抑制所有非空属性,不仅抑制那些我想忽略的属性。假设先前实体的字段 description 为空,则JSON响应将为:

  {
name: abc
}

并且这对于UI来说是有问题的。



那么有没有办法只在JSON响应中忽略该字段?

解决方案

您可以根据需要使用 Jackson JsonView 。您可以为应用程序用户定义一个将用于序列化pojo的视图:



将这些视图作为类创建,一个公共视图,一个私有视图:

 类视图{
静态类Public {}
静态类私有扩展Public {}
}

然后将Pojo中的视图用作注释:

  @Id 
@JsonView(Views.Private.class)字符串名称;
私人Long ID;
@JsonView(Views.Public.class)字符串名称;
private String publicField;

然后使用视图为应用程序用户序列化pojo:

  objectMapper.writeValueUsingView(out,beanInstance,Views.Public.class); 

这是许多其他视图如何适合您的问题的例子。例如。您也可以使用 objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION,false); 排除没有视图注释的字段并删除 Private 视图。


I am developing an application which uses Spring-boot, a relational database and Elasticsearch.

I use JSON serialization at 2 differents places in the code:

  • In the response of the REST API.
  • When the code interacts with Elasticsearch.

There are some properties that I need in Elasticsearch but that I want to hide to the application user (e.g. internal ids coming from the relational database).

Here is an example of entity :

@Document
public class MyElasticsearchEntity {

  @Id
  private Long id; //I want to hide this to the user.
  private String name;
  private String description;
}

Problem : When the object it persisted in Elasticsearch, it gets serialized as JSON. Hence, fields with @JsonIgnore are ignored when serialized to Elasticsearch.

Up to now, I found 2 unsatisfying solutions :

Solution 1 : Use @JsonProperty like this :

@Id
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;

The id gets written in Elasticsearch and is nullified in the JSON response :

{
  "id" : null,
  "name" : "abc",
  "description" : null
}

So it works but the application user still sees that this property exists. This is messy.

Solution 2 : Cutomize the object mapper to ignore null values

Spring-boot has a built-in option for that :

spring.jackson.serialization-inclusion=NON_NULL

Problem : it suppresses all non-null properties, not only those that I want to ignore. Suppose that the field description of the previous entity is empty, the JSON response will be :

{
  "name" : "abc"
}

And this is problematic for the UI.

So is there a way to ignore such field only in the JSON response?

解决方案

You could use Jackson JsonView for your purpose. You can define one view which will be used to serialize pojo for the application user :

Create the views as class, one public and one private:

class Views {
         static class Public { }
         static class Private extends Public { }
}

Then uses the view in your Pojo as an annotation:

@Id
@JsonView(Views.Private.class) String name;
private Long id;
@JsonView(Views.Public.class) String name;
private String publicField;

and then serialize your pojo for the application user using the view:

objectMapper.writeValueUsingView(out, beanInstance, Views.Public.class);

This is one example of many others on how view can fit your question. Eg. you can use too objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false); to exclude field without view annotation and remove the Private view.

这篇关于忽略Json忽略Elasticsearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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