7

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

I have this configuration:

<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>

This Document:

@Document
public class User {
    @Id
    private String id;

    @DBRef
    private Place place;

    @DBRef(lazy=true)
    private Country country;

    .
    .
    .
}

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);    
    }
}

The output is:

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

Questions:

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

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

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

thanks in advance for your help.

Merch0
  • 594
  • 5
  • 17
  • 1
    I think spring-data's lazy loading for mongo doesn't play well with Jackson. Jackson probably uses reflection, and spring-data lazy loading uses CGLIB proxy objects, which probably screws up any reflective actions by Jackson inside spring's (Jackson's) serialization to json. – heez Jun 23 '17 at 19:43
  • Did you ever solve this? – DavidA Mar 28 '18 at 16:07
  • 1
    @DavidA No, I didn't, but I changed the way that I was returning the data. I think that the correct way is returning a DTO instead of exposing directly the entity or mongo document. – Merch0 Mar 30 '18 at 20:29
  • I came across an error where target class was unknown for json-patch. I had no way but to go with lazy=false – Ismail Iqbal Jan 24 '21 at 13:19
  • @Merch0 did you find the answer for question 1? – Baris LaPaz Sep 27 '21 at 12:20

2 Answers2

2

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);
        }
    }
}

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.

DavidA
  • 3,984
  • 5
  • 25
  • 38
  • Tried this on a list but it didn't work @JsonSerialize(using=DBRefSerializer.class) @DBRef(lazy=true) private List repaymentDataList = new ArrayList<>(); Any Suggestions ? – Vamsi Krishna DS Oct 02 '20 at 17:53
-2

Add @JsonIgnore anotation.

@JsonIgnore
@DBRef(lazy = true)
private SomeClass someProperty;

Source : Cooperation MongoDB lazy loading with Jackson @JsonIgnore in SpringBoot Rest Controller

Vamsi Krishna DS
  • 615
  • 6
  • 17
  • I don't know what is understood by you and how come you think solution offered is wrong. Don't be in a perception that your perception is OP's perception. – Vamsi Krishna DS Feb 03 '21 at 14:29