• <bdo id='vZmKL'></bdo><ul id='vZmKL'></ul>

    <legend id='vZmKL'><style id='vZmKL'><dir id='vZmKL'><q id='vZmKL'></q></dir></style></legend><tfoot id='vZmKL'></tfoot>

    <i id='vZmKL'><tr id='vZmKL'><dt id='vZmKL'><q id='vZmKL'><span id='vZmKL'><b id='vZmKL'><form id='vZmKL'><ins id='vZmKL'></ins><ul id='vZmKL'></ul><sub id='vZmKL'></sub></form><legend id='vZmKL'></legend><bdo id='vZmKL'><pre id='vZmKL'><center id='vZmKL'></center></pre></bdo></b><th id='vZmKL'></th></span></q></dt></tr></i><div id='vZmKL'><tfoot id='vZmKL'></tfoot><dl id='vZmKL'><fieldset id='vZmKL'></fieldset></dl></div>
    1. <small id='vZmKL'></small><noframes id='vZmKL'>

        Jackson 使用 mixins 序列化具有动态不同名称的属性

        时间:2023-09-28
        • <tfoot id='KW57D'></tfoot>
          <i id='KW57D'><tr id='KW57D'><dt id='KW57D'><q id='KW57D'><span id='KW57D'><b id='KW57D'><form id='KW57D'><ins id='KW57D'></ins><ul id='KW57D'></ul><sub id='KW57D'></sub></form><legend id='KW57D'></legend><bdo id='KW57D'><pre id='KW57D'><center id='KW57D'></center></pre></bdo></b><th id='KW57D'></th></span></q></dt></tr></i><div id='KW57D'><tfoot id='KW57D'></tfoot><dl id='KW57D'><fieldset id='KW57D'></fieldset></dl></div>
            <bdo id='KW57D'></bdo><ul id='KW57D'></ul>

                    <tbody id='KW57D'></tbody>
                  <legend id='KW57D'><style id='KW57D'><dir id='KW57D'><q id='KW57D'></q></dir></style></legend>

                  <small id='KW57D'></small><noframes id='KW57D'>

                  本文介绍了Jackson 使用 mixins 序列化具有动态不同名称的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我使用不同的 NoSQL 数据库,根据数据库,我需要将id"命名为不同的.因此,例如在 OrientDB 中,id 被命名为@rid"

                  I use different NoSQL databases and depending on the database I need to name the "id" different. So for example in OrientDB the id is named "@rid"

                  @JsonProperty("@rid")
                  private String id;
                  

                  对于 MongoDB,id 被命名为_id"

                  And for MongoDB the id is named "_id"

                  @JsonProperty("@_id")
                  private String id;
                  

                  我不知道现代数据库开发人员有什么问题,而不仅仅是将 id 字段命名为id"^^.但现在我有一个问题.如何动态序列化/反序列化 id 字段,在某些情况下为@rid",在另一种情况下为_id"?

                  I do not know what is wrong with the modern DB developers not just naming the id field "id" ^^. But now I have a problem. How can I dynamically serialize/deserialize the id field in some case as "@rid" and in another case as "_id"?

                  根据 rmullers 的建议,我尝试使用 mixins.所以我有例如:

                  Based on rmullers suggestion I have tried to use mixins. So I have for example:

                  public interface IdMixins {
                  }
                  
                  public interface MongoIdMixIn extends IdMixins {
                      @JsonProperty("_id") String getId();
                      @JsonProperty("_id") void setId(String id);
                  }
                  
                  public interface OrientIdMixIn extends IdMixins{
                      @JsonProperty("@rid") String getId();
                      @JsonProperty("@rid") void setId(String id);
                  }
                  

                  IdMixins 是一个完全空的接口,仅用于更好地控制哪些接口可以传递给映射器.

                  Where IdMixins is a completly empty interface just used to get more controll which interfaces can be passet to the mapper.

                  然后有一个类:

                  @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@javaClass")
                  public abstract class AbstractBean implements Serializable {
                      private static final long serialVersionUID = -1286900676713424199L;
                  
                      // @JsonProperty("@rid")
                      private String id;
                  
                      public String getId() {
                          return id;
                      }
                  
                      public void setId(String id) {
                          this.id = id;
                      }
                  }
                  

                  但是当我运行这个简单的测试时,输出仍然是id":

                  But when I run this simple test, the output is still "id":

                  public class MixinTest {
                      public static void main(String[] args) throws JsonProcessingException {
                          Foo f = new Foo();
                          f.setId("123");
                          f.setBar("lala");
                  
                          ObjectMapper mapper = new ObjectMapper();
                  
                          ObjectMapper m2 = mapper.copy();
                          m2.addMixInAnnotations(AbstractBean.class, MongoIdMixIn.class);
                          System.out.println(m2.writeValueAsString(f));
                  
                          ObjectMapper m3 = mapper.copy();
                          m3.addMixInAnnotations(AbstractBean.class, OrientIdMixIn.class);
                          System.out.println(m3.writeValueAsString(f));
                  
                      }
                  
                      public static class Foo extends AbstractBean {
                          private String bar;
                  
                          public String getBar() {
                              return bar;
                          }
                  
                          public void setBar(String bar) {
                              this.bar = bar;
                          }
                      }
                  }
                  

                  输出:

                  {"@javaClass":"test.MixinTest$Foo","id":"123","bar":"lala","@class":"Foo"}{"@javaClass":"test.MixinTest$Foo","id":"123","bar":"lala","@class":"Foo"}

                  {"@javaClass":"test.MixinTest$Foo","id":"123","bar":"lala","@class":"Foo"} {"@javaClass":"test.MixinTest$Foo","id":"123","bar":"lala","@class":"Foo"}

                  推荐答案

                  你试过使用 http://wiki.fastxml.com/JacksonMixInAnnotations?然后,您可以使用具有不同 @JsonProperty 配置的 OrientDbMixinMongoDbMixin.

                  Have you tried using http://wiki.fasterxml.com/JacksonMixInAnnotations? Then you can use an OrientDbMixin and a MongoDbMixin with different @JsonProperty configuration.

                  更新:工作示例

                  public final class JacksonTest {
                  
                      static final class ExampleBean {
                  
                          private String id;
                          private String bar;
                  
                          @JsonProperty("donotwanttoseethis")
                          public String getId() {
                              return id;
                          }
                  
                          public void setId(String id) {
                              this.id = id;
                          }
                  
                          public String getBar() {
                              return bar;
                          }
                  
                          public void setBar(String bar) {
                              this.bar = bar;
                          }
                  
                      }
                  
                      public interface MongoIdMixIn {
                  
                          @JsonProperty("_id") String getId();
                  
                      }
                  
                      public interface OrientIdMixIn {
                  
                          @JsonProperty("@rid") String getId();
                  
                      }
                  
                      private final static Logger LOG = LoggerFactory.getLogger();
                  
                      public static void main(String[] args) throws JsonProcessingException {
                          ExampleBean bean = new ExampleBean();
                          bean.setId("1234");
                          bean.setBar("lala");
                  
                          ObjectMapper m2 = new ObjectMapper();
                          m2.addMixInAnnotations(ExampleBean.class, MongoIdMixIn.class);
                          LOG.info(m2.writeValueAsString(bean));
                  
                          ObjectMapper m3 = new ObjectMapper();
                          m3.addMixInAnnotations(ExampleBean.class, OrientIdMixIn.class);
                          LOG.info(m3.writeValueAsString(bean));
                      }
                  
                  }
                  

                  这篇关于Jackson 使用 mixins 序列化具有动态不同名称的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MongoDB对同一文档的并发更新不是原子的 下一篇:为高度非规范化的 NoSQL 数据库设计唯一键(主键

                  相关文章

                  最新文章

                  • <bdo id='ZyoYr'></bdo><ul id='ZyoYr'></ul>

                  <i id='ZyoYr'><tr id='ZyoYr'><dt id='ZyoYr'><q id='ZyoYr'><span id='ZyoYr'><b id='ZyoYr'><form id='ZyoYr'><ins id='ZyoYr'></ins><ul id='ZyoYr'></ul><sub id='ZyoYr'></sub></form><legend id='ZyoYr'></legend><bdo id='ZyoYr'><pre id='ZyoYr'><center id='ZyoYr'></center></pre></bdo></b><th id='ZyoYr'></th></span></q></dt></tr></i><div id='ZyoYr'><tfoot id='ZyoYr'></tfoot><dl id='ZyoYr'><fieldset id='ZyoYr'></fieldset></dl></div>

                  <small id='ZyoYr'></small><noframes id='ZyoYr'>

                  1. <tfoot id='ZyoYr'></tfoot>

                      <legend id='ZyoYr'><style id='ZyoYr'><dir id='ZyoYr'><q id='ZyoYr'></q></dir></style></legend>