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.

相关文章