Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose JAX-RS "DTO"s from entities for REST-Clients?

I have a Java EE 6 web application that offers it's data via a JAX-RS REST web service. The entities are annotated with the JPA annotations as well as with the javax.xml.bind JAX annotations. My aim is to assemble a client-jar out of my web-app project that contains the JAX-RS annotated "DTO" classes to be used for JAX unmarshalling in clients of my web-app.

Putting the raw entities in the client jar is not an option because of the JPA annotations, which would lead to bogus dependencies for the client.

Is there a way for doing this without writing the JAX-RS classes twice, for the web-app and the clients?

I thought of annotation processing and killing all JPA annotations in the entities, that's quite techy, but not very handy. My second idea is to extract an interface of the needed getters/setters of the entities. The question here is how to handle the JAX annotations that are placed at the class members and at the getters.

Both ways seem to work somehow. But is there a general purpose solution for that task?

Hint: yes, i'm aware of the way to expose the JPA-Entities directly via rest and its coupling drawbacks to evolution etc =)

like image 670
martin Avatar asked Jan 24 '26 12:01

martin


1 Answers

You could supply the JPA metadata via XML (http://java.sun.com/xml/ns/persistence/orm_2_0.xsd) instead of annotations. This would give you the mapping without the class path dependency.

  • http://java.dzone.com/articles/persisting-entity-classes
like image 178
bdoughan Avatar answered Jan 26 '26 02:01

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!