Sharing Pojos Between Android Project And Java Backend Project
Solution 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
Solution 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.
Solution 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.
Solution 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 objectclassCustomer
{
<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:
publicclassCustomerDTO
{
privateString mName;
publicStringgetName()
{
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.
Post a Comment for "Sharing Pojos Between Android Project And Java Backend Project"