Spring Data Mongo +延迟加载+ REST Jackson [英] Spring Data Mongo + Lazy Load + REST Jackson

查看:139
本文介绍了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. 如果国家"标记为"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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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