Android spring HATEOAS REST API Traverson 客户端支持

Android spring HATEOAS REST API clientside support with Traverson

我正在尝试为具有 HAL 格式响应的超媒体 API 实现 spring android 客户端。 Spring HATEOAS - 参考文档描述了受 Traverson JavaScript 库启发的客户端服务遍历的实现。 我这样做:

    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");

但是我在创建新的 Traverson 对象时遇到以下错误:

java.lang.NoClassDefFoundError: org.springframework.hateoas.hal.HalLinkDiscoverer

有人知道如何解决吗?

在 android 中可能有 other/better 支持 HAL 响应的方法吗?

据我所知,Spring 的 Traverson 实现在 Android 中不可用,因为它是 Spring HATEOAS 模块的一部分,该模块依赖于 spring-core,最终取决于JDK对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.

的实现

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 中,您无法解决 Android 实现不兼容或缺失的 JAXB 或 STaX 等包,因此您必须重写依赖于它们的代码才能使用某些东西否则代替。

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用上面描述的方法,所以也许值得追求?)

Mike Kelly 在他关于 HAL 标准的文档中提供了一个 list of libraries that support HAL. I'm currently putting together a solution based on HalBuilder,到目前为止看起来很有希望。

tl;dr:如果您不付出很多努力,Spring 的 Traverson 将无法工作。您最好自己构建。使用您最喜欢的 HTTP 库和 HAL 库,您就完成了大部分工作。