在Android项目和Java后端项目之间共享POJO [英] Sharing POJOs between Android project and java backend project

查看:135
本文介绍了在Android项目和Java后端项目之间共享POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Android项目和后端项目中共享相同POJO的最佳方法是什么.

I was wondering what would be the best way of sharing same POJOs in Android project and in back-end project.

在后端有POJO的那一刻,此POJO具有Hibernate和Jackson注释.在类的顶部,有HQL句子,如@NameQueries. 当我在Android项目中需要相同的POJO时,请复制POJO并删除所有注释.目前,我在Android项目中使用了GSON.

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.

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

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.

到目前为止,我已经考虑过创建lib项目,但是我还没有弄清楚如何控制注释.在Android方面,我也可以与Jackson一起工作,但是我想摆脱Hibernate的东西.

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.

推荐答案

我可以看到在两者之间共享这些POJO的吸引力,但我认为最好的选择是在共享库中创建一组简单的DTO并使用这些来自后端和android.在两个项目中具有相同的实际域对象的问题正是您所描述的-您正在其中进行后端类型的事情,而这些事情并不属于前端.

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.

一个简单的客户示例(哪种类型与构建器模式有交叉,因此您可以重复使用构建器中的这些DTO)

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;
    }
}

然后,您的DTO在共享库中将是一个简单的版本,并且不需要Jackson注释,因为它会自动默认为仅包含您要在客户端上包含的内容.正如您已经说过的,如果您确实需要一些Jackson注释,那么在Android方面就可以了.这是您将从后端发送回的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;
    }

    ...
}

尽管仍然以重复结束(在适当的域对象和DTO之间),这的确意味着如果您在后端进行任何更改,它也必须在DTO中进行更改,以使客户端可以看到,因此保留客户端同步,因为它使用相同的DTO.

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.

这篇关于在Android项目和Java后端项目之间共享POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆