Sharing POJOs between Android project and java bac

2019-09-17 06:36发布

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.

4条回答
何必那么认真
2楼-- · 2019-09-17 07:09

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-09-17 07:10

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.

查看更多
虎瘦雄心在
4楼-- · 2019-09-17 07:17

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.

查看更多
疯言疯语
5楼-- · 2019-09-17 07:18

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

查看更多
登录 后发表回答