问题
I was wondering what would be the best way of sharing same POJOs in Android project and in back-end project.
At the moment when I have POJO in back-end, then this POJO has Hibernate and Jackson annotations. On top of the class there is HQL sentences as @NameQueries. When I need the same POJO in the Android project then copy the POJO and remove all the annotations. At the moment I use GSON in the Android project.
So my question is that what would be the best way to get rid of this tedious work when I create the new POJO in back-end and I need it in the Android project too.
So far I have thought about the creating lib project but I have not figured out how I can control the annotations. In Android side I can work with Jackson too, but I would like to get rid of Hibernate stuff.
回答1:
i guess GSON would be your best bet. but beware that in hibernate with relationships(one to many, many to many) may cause error due to when parsing to json the get
method of the fields are getting called you need to indicate if the field should be eagerly fetched or lazily fetched. if you dont need the field in the android, you can just use @Expose
annotation in your pojo in backend to indicate which fields should only be expose or converted to json. i think @Expose
is only availbe in google's json
回答2:
Also suggest to take look Jackson it's one of two most popular JSON read/write library (another one GSON is mentioned above).
BiDirectional references with Jackson: http://wiki.fasterxml.com/JacksonFeatureBiDirReferences
Circular references: Json and Java - Circular Reference
Also features like custom serializers and deserializers.
回答3:
The best way is to use a serialization "scheme" (for lack of better word) to serialize your POJO into a serialized stream that can be demarshalled by Android.
The most common format used in modern day programming technology is JSON, so using JSON in mind, you can create a service that will render your POJO to a JSON Object and your Android app will deserialize it from JSON to POJO.
A service can be a Restful Web Service or you can create a custom service that renders your POJO to JSON.
Java 7 now includes JSR-353, JSON Processing (JSON-P for short) and this gives your various functionalities and classes to provide JSON marshalling and unmarshalling. A simple tutorial can be found here.
In your Android app, you can then use GSON to do JSON unmarshalling.
I hope this help.
回答4:
I can see the attraction of sharing these POJOs across the two but I think the best option would be to create a simple set of DTOs in a shared lib and use these from the back end and android. The problem with having the same actual domain objects in both projects is exactly what you've described - that you're doing back end type things in them that don't belong in the front end.
A simple example for a Customer (which kind of has some cross over with the builder pattern so you could re-use these DTO's from a builder):
//this is your back end "proper" domain object
class Customer
{
<back end specifics>
...
Customer(CustomerDTO customer)
{
//set Customer fields from the dto
}
CustomerDTO toDTO()
{
CustomerDTO dto = new CustomerDTO();
dto.setName(this.getName());
dto.setAddress(this.getAddress());
...
return dto;
}
}
Then your DTO would be a simple version in your shared library and shouldn't need Jackson annotations as it'll automatically default to including only what you want on the client. As you've said though, if you do need some Jackson annotations that's fine on the Android side. It's the DTO you'd send back from the back end:
public class CustomerDTO
{
private String mName;
public String getName()
{
return mName;
}
...
}
Whilst that still ends up with duplication (between the proper domain object and the DTO) it does mean if you change anything in the back end it must also change in the DTO to be seen by the client and the client therefore is kept in sync cause it uses the same DTO.
来源:https://stackoverflow.com/questions/25640838/sharing-pojos-between-android-project-and-java-backend-project