简体   繁体   English

杰克逊2的JSON,我需要类似XMLAdapter的功能

[英]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. 之前,我已经使用来自数据库的XML响应创建了服务,并且我成功加载了所有实体并将其与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. 因此,我现在正在使用JSON,并且无法使Hibernate Collections正常工作。 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. 因此,我不明白……虽然fe主管部门可能没事,但是fe会议正在收集其所有馆藏,并且以后所有这些都会影响“ rob”用户,因为实体没有加倍。

I think its because I use @JsonManagedReference and @JsonBackReference but I do not know if correctly. 我认为是因为我使用@JsonManagedReference@JsonBackReference但是我不知道是否正确。

This is great article which helped me to understand this issue; 这篇很棒的文章帮助我理解了这个问题; read carefully! 仔细阅读!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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