简体   繁体   English

在Android项目和Java后端项目之间共享POJO

[英]Sharing POJOs between Android project and java backend project

I was wondering what would be the best way of sharing same POJOs in Android project and in back-end project. 我想知道在Android项目和后端项目中共享相同POJO的最佳方法是什么。

At the moment when I have POJO in back-end, then this POJO has Hibernate and Jackson annotations. 在后端有POJO的那一刻,此POJO具有Hibernate和Jackson注释。 On top of the class there is HQL sentences as @NameQueries. 在类的顶部,有HQL句子,如@NameQueries。 When I need the same POJO in the Android project then copy the POJO and remove all the annotations. 当我在Android项目中需要相同的POJO时,请复制POJO并删除所有注释。 At the moment I use GSON in the Android project. 目前,我在Android项目中使用GSON。

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. 所以我的问题是,当我在后端创建新的POJO并且在Android项目中也需要它时,摆脱这种繁琐工作的最佳方法是什么?

So far I have thought about the creating lib project but I have not figured out how I can control the annotations. 到目前为止,我已经考虑过创建lib项目,但是还没有弄清楚如何控制注释。 In Android side I can work with Jackson too, but I would like to get rid of Hibernate stuff. 在Android方面,我也可以与Jackson一起工作,但是我想摆脱Hibernate的东西。

i guess GSON would be your best bet. 我想GSON将是您最好的选择。 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. 但请注意,在休眠时(一对多,多对多)的关系可能会由于在解析json时调用字段的get方法而导致错误,您需要指出是应该急于获取字段还是应延迟获取字段。 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. 如果您不需要android中的字段,则可以在后端的pojo中使用@Expose批注来指示哪些字段仅应公开或转换为json。 i think @Expose is only availbe in google's json 我认为@Expose仅在Google的json中可用

Also suggest to take look Jackson it's one of two most popular JSON read/write library (another one GSON is mentioned above). 还建议您看一下Jackson,它是两个最受欢迎的JSON读/写库之一(上面提到了另一个GSON)。

BiDirectional references with Jackson: http://wiki.fasterxml.com/JacksonFeatureBiDirReferences 与Jackson的双向引用: http ://wiki.fasterxml.com/JacksonFeatureBiDirReferences

Circular references: Json and Java - Circular Reference 循环引用: Json和Java-循环引用

Also features like custom serializers and deserializers. 还具有自定义序列化器和反序列化器等功能。

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. 最好的方法是使用序列化“方案”(由于缺少更好的单词)将POJO序列化为可以由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. 现代编程技术中最常用的格式是JSON,因此请牢记JSON,您可以创建一个将POJO呈现为JSON对象的服务,然后您的Android应用会将其从JSON反序列化为POJO。

A service can be a Restful Web Service or you can create a custom service that renders your POJO to JSON. 服务可以是Restful Web服务,也可以创建将POJO呈现为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. Java 7现在包括JSR-353,JSON处理(简称JSON-P),这为您提供了各种功能和类来提供JSON编组和解编。 A simple tutorial can be found here . 一个简单的教程可以在这里找到。

In your Android app, you can then use GSON to do JSON unmarshalling. 然后,您可以在Android应用中使用GSON进行JSON解组。

I hope this help. 希望对您有所帮助。

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. 我可以看到在两者之间共享这些POJO的吸引力,但我认为最好的选择是在共享库中创建一组简单的DTO,并从后端和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): 客户的一个简单示例(哪种类型与构建器模式有交叉,因此您可以重用来自构建器的这些DTO):

//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. 然后,您的DTO在共享库中将是一个简单的版本,并且不需要Jackson注释,因为它会自动默认为仅包含客户端上想要的内容。 As you've said though, if you do need some Jackson annotations that's fine on the Android side. 正如您已经说过的,如果您确实需要一些Jackson注释,那么在Android方面就可以了。 It's the DTO you'd send back from the back end: 这是您从后端发回的DTO:

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. 虽然这仍然会导致重复(在适当的域对象和DTO之间),但这确实意味着,如果您在后端进行任何更改,它也必须在DTO中进行更改,以使客户端可以看到,因此客户端保持同步。它使用相同的DTO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM