0

I'm running a SpringBoot + MongoDB project where I have tons of object references in my database. In my controllers I would like to output objects in JSON with "proxy" placeholders instead of the real objects in the references.

For example I am currently getting the following output

"inputThing" : {
  "name" : "some name",
  "length" : 16,
  "formats" : [ 
  {
    "format" : "decimal",           <-- "_id" : "1"
    "type" : "some type"
  }, {
    "format" : "octal",             <-- "_id" : "2"
    "type" : "some other type"
  }, {
    "format" : "hexadecimal",       <-- "_id" : "3"
     "type" : "yet another type"
  } ]
}

and I would instead like to have this output with "proxy" references

  "inputThing" : {
  "name" : "some name",
  "length" : 16,
  "formats" : [ 
  {
    "_id" : "1"
  }, {
    "_id" : "2"
  }, {
    "_id" : "3"
  } ]
}

So that the produced json is less heavy, and clients can get the object with id "1" using another controller handler.

Any ideas how to do this? Thx

Jerome
  • 1,225
  • 2
  • 12
  • 23
  • Possible duplicate of [How to serialize only the ID of a child with Jackson](https://stackoverflow.com/questions/17542240/how-to-serialize-only-the-id-of-a-child-with-jackson) – Dovmo Jan 11 '19 at 05:16
  • This helps yes but I'd like to know if there is a dynamic way to do it, i.e. without hardcoded annotations in my classes ? – Jerome Jan 11 '19 at 09:27

0 Answers0