使用Traverson的基于HAL的REST服务客户端

时间:2018-12-05 16:49:54

标签: java rest client spring-hateoas

我试图编写一个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具有要跟随的链接?

0 个答案:

没有答案
相关问题