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

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

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

      2. Spring Data Mongo + 延迟加载 + REST Jackson

        时间:2024-08-23

          <tbody id='WaSzp'></tbody>

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

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

            <bdo id='WaSzp'></bdo><ul id='WaSzp'></ul>
                <legend id='WaSzp'><style id='WaSzp'><dir id='WaSzp'><q id='WaSzp'></q></dir></style></legend>
                <tfoot id='WaSzp'></tfoot>

                1. 本文介绍了Spring Data Mongo + 延迟加载 + REST Jackson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好,我对带有延迟加载的 Spring 和 Mongo 有一些问题.

                  Hello I have some issues with Spring and Mongo with Lazy Load.

                  我有这个配置:

                  <parent>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-parent</artifactId>
                      <version>1.5.1.RELEASE</version>
                  </parent>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-data-mongodb</artifactId>
                  </dependency>
                  

                  本文档:

                  @Document
                  public class User {
                      @Id
                      private String id;
                  
                      @DBRef
                      private Place place;
                  
                      @DBRef(lazy=true)
                      private Country country;
                  
                      .
                      .
                      .
                  }
                  

                  一切正常,但是当我在 RestController 中公开用户"时,例如:

                  Everything works fine, but when I expose the "User" in a RestController, for example:

                  @RestController
                  public class UserController {
                  
                      .
                      .
                      .
                  
                      @RequestMapping(value = "user/{idUser}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
                      public User getById(@PathVariable("idUser") String idUser){    
                              return userService.getById(idUser);    
                      }
                  }
                  

                  输出是:

                    {
                      "id": "58ebf11ee68f2751f33ae603",
                      "place": {
                        "id": "58e3bf76e76877586435f5af",
                        "name": "Place X"
                      },
                      "country": {
                        "id": "58daa782e96139070bbc851c",
                        "name": "México",
                        "target":{
                          "id": "58daa782e96139070bbc851c",
                          "name": "México",
                        }
                      }
                    }
                  

                  问题:

                  1. 如果country"标记为lazy=true",为什么会打印出来?

                  1. If "country" is marked as "lazy=true", why it is printed out?

                  为什么在国家"中有一个名为目标"的新字段?

                  Why there is a new field named "target" in "country"?

                  如何避免序列化标记为lazy=true"的字段?

                  How can I avoid serialize fields marked as "lazy=true"?

                  提前感谢您的帮助.

                  推荐答案

                  我遇到了类似的问题,目标"出现在序列化结果中,并且能够通过创建自定义序列化程序来解决这个问题,以便序列化实际对象,而不是代理,它具有目标"字段并具有时髦的类名.

                  I had a similar issue with the 'target' showing up in the serialized results, and was able to solve this issue by creating a custom serializer so it serializes the actual object, and not the proxy, which has the 'target' field and has a funky class name.

                  显然,您可以选择不获取目标,而不是简单地获取它并对其进行序列化,如下所示:

                  Obviously, you could choose to not fetch the target instead of simply fetching it and serializing it as is shown here:

                  public class DBRefSerializer extends JsonSerializer<Object> {
                  
                      @Override
                      public void serialize(Object value, JsonGenerator generator, SerializerProvider provider)
                              throws JsonGenerationException, IOException {
                  
                          provider.defaultSerializeValue(value, generator);
                      }
                  
                      @Override
                      public void serializeWithType(Object value, JsonGenerator generator, SerializerProvider provider,
                              TypeSerializer typeSer)
                              throws IOException {
                  
                          Object target = value;
                          if (value instanceof LazyLoadingProxy) {
                              LazyLoadingProxy proxy = (LazyLoadingProxy)value;
                              target = proxy.getTarget();
                              provider.defaultSerializeValue(target, generator);
                          } else {
                              provider.defaultSerializeValue(target, generator);
                          }
                      }
                  }
                  

                  然后像这样注释您想要运行的 DBRef:

                  And then annotate the DBRefs you want to run through it like this:

                  @JsonSerialize(using=DBRefSerializer.class)
                  @DBRef(lazy = true)
                  private SomeClass someProperty;
                  

                  显然这并不适合所有人,但我想我会发布它以防它帮助其他人.

                  Obviously this isn't perfect for everyone, but I figured I would post it in case it helps someone else.

                  这篇关于Spring Data Mongo + 延迟加载 + REST Jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在没有 _class 属性的情况下将 spring 数据与 couchbase 一起使用 下一篇:如何使用 java config 而不是 XML 声明一个存储库填充器 bean?

                  相关文章

                    <bdo id='TFSKz'></bdo><ul id='TFSKz'></ul>
                2. <small id='TFSKz'></small><noframes id='TFSKz'>

                  1. <legend id='TFSKz'><style id='TFSKz'><dir id='TFSKz'><q id='TFSKz'></q></dir></style></legend>

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