im trying to implement an spring android client for a Hypermedia API with responses in HAL format. The Spring HATEOAS - Reference Documentation described the implementation with client side service traversal inspired by the Traverson JavaScript library. I do this this way:
Traverson traverson = null;
try {
traverson = new Traverson(new URI(getString(R.string.api_test_uri)), MediaTypes.HAL_JSON);
} catch (URISyntaxException e) {
e.printStackTrace();
}
String name = traverson.follow("movies", "movie", "actor").
withTemplateParameters(parameters).
toObject("$.name");
But i'm getting following error while creating a new Traverson object:
java.lang.NoClassDefFoundError: org.springframework.hateoas.hal.HalLinkDiscoverer
Do somebody know how to fix it?
Is there probably other/better ways to support HAL responses in android?
As best I can tell, Spring's Traverson implementation is not usable in Android, because it is part of the Spring HATEOAS module, which depends on spring-core, which ultimately depends on the JDK's implementation of StAX. Android doesn't have a StAX implementation, and because it's in a javax.*
package, the Android runtime won't allow you to load one.
In a blog post, Josh Long describes the process of adapting Spring Social and Spring Security to work on Android; essentially, you have to strip out most of their dependencies and selectively re-add only the ones you need. However, you can't work around packages like JAXB or STaX whose Android implementations are incompatible or missing, so you have to rewrite the code that depends on them to use something else instead.
A request has been posted against the spring-hateoas project to support Android, but it was closed (two days ago as I write this) with the comment "we can't commit resources to such a platform specific feature at this time". (On the other hand, someone else seems to be having success with the method described above, so maybe it's worth pursuing?)
Mike Kelly, in his documentation on the HAL standard, supplies a list of libraries that support HAL. I'm currently putting together a solution based on HalBuilder, which looks promising so far.
tl;dr: Spring's Traverson won't work without a lot of effort on your part. You may be better off building your own. Use your favorite HTTP library along with a HAL library, and you're most of the way there.