简体   繁体   English

Java客户端,用于将复杂的实体发布到Spring Data REST / HATEOAS服务

[英]Java Client for POSTing complex entities to a Spring Data REST / HATEOAS service

From what I can tell, there are provided means for converting a complex object to proper HAL format. 据我所知,提供了将复杂对象转换为正确的HAL格式的方法。 This is of course leveraged in marshalling the objects in the framework itself. 当然,这可以利用在框架本身中编组对象。 Resource and Link objects, etc. ResourceLink对象等

For the sake of a use-case: Company 1 is an existing Company in my system. 出于用例考虑: Company 1是我系统中的现有Company I want to add a new Employee that works for Company 1 我想添加一个适用于Company 1的新Employee

Below is an example Employee object that you'd receive from a Spring Data REST based service. 以下是您将从基于Spring Data REST的服务中收到的示例Employee对象。 Spring HATEOAS also provides the means to construct these objects yourself. Spring HATEOAS还提供了自己构造这些对象的方法。

{
    "id": null,
    "firstName": "bZWthNFk",
    "lastName": "GtTnrqka",
    "loginId": "zTk5rT",
    "active": true,
    "_links": {
        "company": {
            "href": "http://localhost/companies/1";
        }
    }
}

However, this seems to not work for POSTing the object. 但是,这似乎不适用于POST对象。 As I understand it, that same object would have to be POSTed as: 据我了解,该对象必须发布为:

{
    "id": null,
    "firstName": "bZWthNFk",
    "lastName": "GtTnrqka",
    "loginId": "zTk5rT",
    "active": true,
    "company": "http://localhost/companies/1"
}

As far as I can tell, there are no means provided by either the HATEOAS or Data REST project to produce this object for posting to a valid HAL based service, either via RestTemplate or some other means. 据我所知,没有通过无论是HATEOAS或数据REST项目提供制造这种对象发布到一个有效的HAL基础的服务,无论是通过手段RestTemplate或其他方式。 In fact, I can't find any means of readily POSTing a complex object without some hand-marshalling. 实际上,如果没有手动编组,我找不到任何容易地发布复杂对象的方法。 Am I wrong in assuming this? 我承担这个错吗?

How is one supposed to build out a valid Java SDK for service-to-service communication that leverages HATEOAS principles without this tooling to actually POST objects reliably? 应该如何构建一个有效的Java SDK,以实现利用HATEOAS原理的服务到服务通信,而无需使用此工具来可靠地实际POST对象?


Long story short, I want to post this object without having to hand serialize the URIs for associations. 长话短说,我想发布该对象而不必手动序列化关联的URI。

public class Employee {
    private Integer id;
    @NotNull
    private Company company;
    private String firstName;
    private String lastName;
}

I've created the following improvement request in reference to this: 为此,我创建了以下改进请求:

https://jira.spring.io/browse/SPR-12678 https://jira.spring.io/browse/SPR-12678

The approach you suggested should actually work, provided you use at least version 2.0 of Spring Data REST. 如果您至少使用Spring Data REST 2.0版,那么您建议的方法应该可以实际使用。

You should also have an association resource like http://app.com/employee/10/company . 您还应该拥有一个关联资源,例如http://app.com/employee/10/company You can PUT a new link to that location using the media type text/uri-list or remove the company from Employee with a DELETE . 您可以PUT使用的媒体类型的新链接到该位置text/uri-list或删除公司EmployeeDELETE

UDATE UDATE

It seems I didn't address your main concern, that was clarified by your update and comments. 看来我没有解决您的主要问题,但您的更新和评论已表明了这一点。 So let's take your Employee class that has an association with a Customer . 因此,让我们来看看与Customer有关联的Employee类。

As you can see from the JSON response you posted, the data structure that the REST API works with doesn't contain a Customer object (or Company in that case), only a link. 从发布的JSON响应中可以看到,REST API使用的数据结构不包含Customer对象(在这种情况下为Company ),仅包含链接。 A client would usually work with the data structure defined by the API. 客户端通常会使用API​​定义的数据结构。 So customer would be link in the first place and there would be no need for serializing an object to a link. 因此, customer将首先处于链接状态,并且无需将对象序列化为链接。

If the client uses a different data structure internally, then some kind of conversion is necessary anyway. 如果客户端内部使用其他数据结构,则无论如何都需要某种转换。 But the reason would be the different structure, not HAL or association links. 但是原因可能是结构不同,而不是HAL或关联链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM