1

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.

Cédric B.
  • 21
  • 4

1 Answers1

0

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 POJOs 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 POJOs 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.
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • 1
    Thank you for your answer. I think that there is Pros and Cons on different ways to manage it but not a best practice at this time (we have to find our own one) and your answer confirm it. I will check and read each of your link with attention (I already red some before posting but I was a bit lost) and make the choice. – Cédric B. Feb 21 '19 at 10:29
  • @CédricB., how you solved your problem? What did you choose? Was my answer helpful? – Michał Ziober Oct 08 '19 at 21:18
  • 1
    @ZioberMichal sorry for the late answer. We have finally removed the XML parsing, and we use jackson for JSON parsing. It's much more light when sending data. – Cédric B. May 19 '21 at 15:06