简体   繁体   中英

JSON with Jackson 2, i need XMLAdapter like functionality

So I am struggling with this for a week or so. I previously have created a service with XML response from Database and I successfully loaded all the entities and used them with XMLAdapters.

This worked for both read, write, create and delete. Especially fragile moment were collections, but the Adapters make them work really nice.

So I am working with JSON now and I cant make the Hibernate Collections to work properly. I either get full-recursive eagerly loaded collections which implicts in sending the whole database in most cases, or in place of collections :(.

What is the approach I should take? Thanks in advance!


an example response, listing all the users:

[
   {
      "username":"bob",
      "password":"8ee3086749f7fa95ffe9c4588037cb10",
      "enabled":true,
      "email":null,
      "name":null,
      "version":1,
      "authorities":[
         {
            "id":3,
            "version":0,
            "description":null,
            "comment":null,
            "authority":"ROLE_USER"
         },
         {
            "id":2,
            "version":0,
            "description":null,
            "comment":null,
            "authority":"ROLE_ADMIN"
         }
      ],
      "messages":[
         {
            "id":2,
            "version":0,
            "description":null,
            "comment":null,
            "message":"TEST BACK"
         }
      ],
      "meetings":[
         {
            "id":1,
            "version":0,
            "description":null,
            "comment":null,
            "messages":[
               2,
               {
                  "id":1,
                  "version":0,
                  "description":null,
                  "comment":null,
                  "message":"TEST"
               }
            ]
         }
      ]
   },
   {
      "username":"rod",
      "password":"e97673c55fc4f2af8bf2122333df24ac",
      "enabled":true,
      "email":null,
      "name":null,
      "version":1,
      "authorities":[
         {
            "id":1,
            "version":0,
            "description":null,
            "comment":null,
            "authority":"ROLE_USER"
         }
      ],
      "messages":[
         1
      ],
      "meetings":[
         1
      ]
   }
]

So I do not understand... while fe authorities could be allright, but then fe meeting is being loaded with all of its collections and that all affects "rob" user later on because the entites are not doubled.

I think its because I use @JsonManagedReference and @JsonBackReference but I do not know if correctly.

This is great article which helped me to understand this issue; read carefully!

http://www.javacodegeeks.com/2011/10/avoid-lazy-jpa-collections.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM