首页 > 解决方案 > 使用 Traverson 的基于 HAL 的 REST 服务客户端

问题描述

我正在尝试编写一个 Java 程序来使用我使用 spring.io 上的教程编写的基于 REST 的 Web 服务的输出。当我运行 SpringBoot bootRun 时,JSON 输出是用嵌入的数据和链接很好地构建的,看起来像这样:

 {
"_embedded": {
    "invoiceList": [
        {
            "id": 4,
            "seqNum": 1,
            "fileId": null,
            "fileName": null,
            "invoiceNumber": "10080",
            "invoiceDate": "2018-06-18T05:00:00.000+0000",
            "invoiceTotal": 1000,
            "sourceLastModified": "2018-11-30T16:22:23.000+0000",
            "lastModified": "2018-11-30T16:22:23.000+0000",
            "validFrom": "2018-11-30T16:22:23.000+0000",
            "validTo": "9999-12-31T06:00:00.000+0000",
            "_links": {
                "self": {
                    "href": "http://localhost:8086/vadir/dental/invoices/4"
                },
                "invoices": {
                    "href": "http://localhost:8086/vadir/dental/invoices"
                },
                "claims": {
                    "href": "http://localhost:8086/vadir/dental/claims?invoice=10080"
                }
            }
        },
        {
            "id": 5,
            "seqNum": 1,
            "fileId": null,
            "fileName": null,
            "invoiceNumber": "10080",
            "invoiceDate": "2018-06-18T05:00:00.000+0000",
            "invoiceTotal": 500,
            "sourceLastModified": "2018-11-30T16:22:23.000+0000",
            "lastModified": "2018-11-30T16:22:23.000+0000",
            "validFrom": "2018-11-30T16:22:23.000+0000",
            "validTo": "9999-12-31T06:00:00.000+0000",
            "_links": {
                "self": {
                    "href": "http://localhost:8086/vadir/dental/invoices/5"
                },
                "invoices": {
                    "href": "http://localhost:8086/vadir/dental/invoices"
                },
                "claims": {
                    "href": "http://localhost:8086/vadir/dental/claims?invoice=10080"
                }
            }
        }
    ]
},
"_links": {
    "self": {
        "href": "http://localhost:8086/vadir/dental/invoices/last"
    }
}

}

我在Consuming HAL-based REST中找到了 Traverson 的文档和 SO 问题。但是,当我使用

ParameterizedTypeReference<Resources<Resource<DentalInvoice>>> parameterizedTypeReference =
        new ParameterizedTypeReference<Resources<Resource<DentalInvoice>>> () {
        };
    Traverson traverson =
        new Traverson (new URI ("http://localhost:8086/vadir/dental/invoices/last"), MediaTypes.HAL_JSON);
    Resources<Resource<DentalInvoice>> invoiceResources = traverson
                                                    //.follow ((String) null)
                                                    .follow ("$._embedded.invoiceList")
                                                    .toObject (parameterizedTypeReference);

我收到一个错误:索引 0 处的方案名称中存在非法字符:[{"id":4,...

看起来 Traverson 期望跟随引用的内容是链接而不是嵌入对象。如何让 Traverson 将嵌入对象解析为 DentalInvoices 资源列表?

我是否必须创建一个仅输出可能操作的链接的控制器方法,以便 Traverson 有一个链接可以关注?

标签: javarestclientspring-hateoas

解决方案


推荐阅读