Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Mongo + Lazy Load + REST Jackson

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.

like image 534
Merch0 Avatar asked Apr 18 '17 18:04

Merch0


1 Answers

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.

like image 107
DavidA Avatar answered Oct 25 '22 21:10

DavidA