简体   繁体   English

杰克逊(JSON库)可以自动生成POJO进行反序列化吗?

[英]Can Jackson (JSON library) autogenerate POJO for deserialization?

I'm using Jackson in a Spring application to deserialize JSON streams (coming from REST services) into POJOs. 我在Spring应用程序中使用Jackson将JSON流(来自REST服务)反序列化为POJO。 At the moment I use this web utility to generate the POJO from the JSON stream. 目前,我使用 Web实用程序从JSON流生成POJO。 The point is that in the future I will handle different JSON streams and I think it will become more and more complex and frustrating to generate POJOs for the different JSON streams. 关键是,将来我会处理不同的JSON流,我认为为不同的JSON流生成POJO会变得越来越复杂和令人沮丧。

Is there any way to let the project auto-generate the POJO starting from the JSON stream? 有没有办法让项目从JSON流开始自动生成POJO? For example the QueryDSL library provides a Maven plugin to generate the Q* classes used for query building. 例如, QueryDSL库提供了一个Maven插件来生成用于查询构建的Q *类。 Does it exist a similar plugin for Jackson or any other way to make things done? 是否存在杰克逊的类似插件或任何其他方式来完成任务?

Thank you in previous 谢谢你以前

Giulio 朱利奥

The jackson documentation makes no mention of anything similar and I have never heard of anything like what you mention. 杰克逊的文档没有提到类似的东西,我从来没有听说过你提到的任何东西。

You mentioned QueryDSL which generates the Q* classes during the build phase. 您提到了在构建阶段生成Q *类的QueryDSL。 That means that you are willing take use extra steps to build your code, which means that you should't have any troubles using jsonschema2pojo 's Maven or Gradle plugins 这意味着你愿意采取额外的步骤来构建你的代码,这意味着使用jsonschema2pojoMavenGradle插件你不应该有任何麻烦

Maybe there is something wrong with your REST design. 也许你的REST设计有问题。 REST endpoints should be very specific. REST端点应该非常具体。 Each rest endpoint should represent a resource, in other words, a domain/pojo that usually represents an entity of your data base. 每个休息端点应代表一个资源,换句话说,一个域/ pojo通常代表您的数据库的实体。

If your endpoint receive more than one resource representation, you could end up with a huge DTO, very hard to maintain. 如果您的端点收到多个资源表示,最终可能会出现一个巨大的DTO,很难维护。

Usually, you build your domain model. 通常,您构建域模型。 These domains are regular pojos, so you would use them to convert json/java - java/json. 这些域是常规的pojos,所以你可以用它们来转换json / java - java / json。 This should scale well. 这应该很好地扩展。 Any modification to your model should affect your json stream as well. 对模型的任何修改也会影响您的json流。 If you modify a domain, only the endpoint that represents this resource will take effect. 如果修改域,则只有表示此资源的端点才会生效。

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

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