java - 使用 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 有一个链接可以关注?
解决方案
推荐阅读
- css - 根据img覆盖svg响应
- r - 如何在 ggplot2 中保存带有特殊字符的 R 图?
- .net - 无法在 VS2019 中构建 CSCORE - 后期构建中的 ilasm 失败
- optaplanner - 计划变量不支持收集
- sql - 我如何不在此 SQL 查询中重复自己?
- apache-spark - 如何让我的多连接/多联合数据集计算得更快?
- git - 添加功能分支以分解 master
- chef-infra - 如何在 Chef's Test Kitchen 中为 virtualbox vm 设置磁盘大小
- python-3.x - 大 O 符号 | 时间复杂度 - 带有“and”关键字的嵌套循环
- javascript - 为什么悬停在我的 d3 地图上无法正常工作?