Is there a way to expose Hibernate Entities as RESTful resources without DTOs?

南笙酒味 提交于 2019-11-29 07:18:56

I have had an analogous problem with passing Hibernate'd VO's back and forth in GWT applications, and in some projects used Dozer to good effect, and in other projects used the approach described in this article, which basically null's the hibernate proxies before marshalling.

Hope that helps you,

I might sound too conservative but I consider using DTOs still a good idea.

The complexity of your mappings is proportional to the granularity of your resources' API, in other words the coarser the more complex.

Take a loot at this class: https://github.com/viniciuspires/reqlist/blob/master/src/main/java/org/reqlist/arch/HibernateAwareObjectMapper.java

I'm using Jackson as a JSON serializer/deserializer, and I had to make this class and add the Hibernate4Module for it to verify if Hibernate.isInitialized and don't accidentaly initialize the property.

After that you just have to configure it as your ObjectMapper, and pass it to the MessageConverters array, as I did in this line:

https://github.com/viniciuspires/reqlist/blob/master/src/main/resources/META-INF/org.reqlist.context.xml#L21

That worked like a charm for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!