简体   繁体   中英

What is the best way to parse data using same API between JEE Restfull webservice and Android?

We have a big project which is composed by JEE modules, JAVA client applications, Android applications, and other self-made java projects.

Because of the variety of projects, we decided to make java libs projects and java entity projects which are common to JEE, Java client applications and Android applications in the goal of limit code redundancy between projects.

At the beginning, we only had Java Clients and Restfull web services on the JEE server side which were exchanging data using JAXB XML Binding API. So it was easy to use JAXB annotations on our Classes in the entity project (which is set as dependency on Java Client project and JEE projects). Both sides could easily encode and decode XML data with the same annotations.

Now that we have an Android app, I wanted to use the same way to exchange data. The thing is that JAXB is 'depreciated' on Android. So I found in Jackson lib that there is a JaxbAnnotation parameter for getting data which is bind with JAXB but I'm not convinced by the full compatibility of the solution.

I also tried using JSON binding since I saw that JSON-B will be the standard in JavaEE 8 but it seems that it needs JavaEE API classes and I don't think that it's good to add it to Android project.

So my question is: What is the best practice to exchange data between JEE Restfull web services and Android application using the same entity dependency (and same parsing API) and limiting the XML or JSON binding annotation on the entity objects?

I hope that you will well understand the problem.

Thank you.

Let's name your entities project entities-module . This project contains POJO classes annotated with JAXB annotations such as: @XmlElement , @XmlType , etc. Since, like you wrote, " JAXB is 'depreciated' on Android " you need to choose between: read JAXB annotations by other tools or create new customised POJO structure.

Read JAXB annotations by other tools

Jackson library has good support for JAXB annotations. All you need to do is to use jackson-module-jaxb-annotations module. It is easy to register and if you already use Jackson for serialising/deserialising JSON this is obvious choice. How to do that you can find reading answers for this question on SO: Using JAXB with Google Android .
Pros:

  • You do not need to create new POJO s schema.
  • In case of changes in Restful API you use next version of entities-module project.

Cons:

  • Problems with enabling JAXB on Android .
  • Performance issues linked with using heavy JAXB and Jackson module layers.

New module

Second choice is to create new module with POJO s and use faster and smaller library like SimpleXML .
Pros:

  • Better performance
  • No problems with building app with depreciated JAXB classes.
  • You can optimise structure: ignore unused fields, choose better types, etc.

Cons:

  • You need to create new module with new classes structure.
  • Learn and maintain new library
  • In case of changes in API you need to duplicate changes in few modules.

You need to take a look on above pros and cons list and decide what is the best for you.

I would like to also add two more options:

  1. Replace JAXB annotations with Jackson annotations. Jackson has really good XML support beside great for JSON . It will enable for you easy support for JSON and XML formats in your API . Also, you can use Jackson library on Android .
  2. Create new JSON API for Android app. Mostly, UI flows on Android app is different than on Web App and you will, probably, end up with that idea anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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