简体   繁体   English

Spring HATEOAS错误的自我链接与链接的资源

[英]Spring HATEOAS wrong self link with linked resources

I'm using Spring Boot 2.0.3, Spring Data REST, Spring HATEOAS. 我正在使用Spring Boot 2.0.3,Spring Data REST,Spring HATEOAS。 My domain model is quite structured but lately I found a strange behaviour in self links. 我的域名模型非常有条理,但最近我在自我链接中发现了一个奇怪的行为。

I'm going to show part of the model to point out the problem, removing useless part: 我将展示模型的一部分以指出问题,删除无用的部分:

EyeExam: EyeExam:

@EntityListeners(value = EyeExamListener.class)
public class EyeExam extends AbstractEntity {

    @NotNull
    @JoinColumn(name = "contact_id", updatable = false)
    @JsonDeserialize(using = ContactUriDeserializer.class)
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Contact contact;

    @NotNull
    @Column(nullable = false, columnDefinition = "DATE")
    private Instant date;

Contact: 联系:

@EntityListeners({ContactListener.class})
public class Contact extends AbstractEntity {

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(nullable = false, columnDefinition = "VARCHAR(30) DEFAULT 'CUSTOMER'")
    private ContactType type = ContactType.CUSTOMER;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(nullable = false, columnDefinition = "VARCHAR(30) DEFAULT 'NATURAL_PERSON'")
    private PersonType personType = PersonType.NATURAL_PERSON;

    private String firstName;

    private String lastName;

    private String companyName;

This is ContactRepository : 这是ContactRepository

@Transactional
@PreAuthorize("isAuthenticated()")
public interface ContactRepository extends JpaRepository<Contact, Long> {
 ....
 ....
}

When I retrieve a specific EyeExam resource ( https://myserver.com/api/v1/eyeExams/13 ) Spring returns: 当我检索特定的EyeExam资源( https://myserver.com/api/v1/eyeExams/13 )时,Spring返回:

  {
  "sid" : "f16d6e45-477f-11e9-898e-9d6f4f2f5990",
  "createdBy" : "system",
  "createdDate" : "2017-05-31T17:38:00Z",
  "lastModifiedDate" : null,
  "lastModifiedBy" : null,
  "createdByName" : "System",
  "lastModifiedByName" : null,
  "date" : "2017-05-31T00:00:00Z",

  "_links" : {
    "self" : {
      "href" : "https://myserver.com/api/v1/eyeExams/13"
    },
    "eyeExam" : {
      "href" : "https://myserver.com/api/v1/eyeExams/13{?projection}",
      "templated" : true
    },
    "supplyTypes" : {
      "href" : "https://myserver.com/api/v1/eyeExams/13/supplyTypes"
    },
    "changeStatus" : {
      "href" : "https://myserver.com/api/v1/eyeExams/13/changeStatus?status=%7Bstatus%7D"
    },
    "contact" : {
      "href" : "https://myserver.com/api/v1/eyeExams/13/contact{?projection}",
      "templated" : true
    },
    "store" : {
      "href" : "https://myserver.com/api/v1/eyeExams/13/store{?projection}",
      "templated" : true
    }
  }
}

As you can see, I get a link to linked resource Contact. 如您所见,我获得了到链接资源联系人的链接。 That's ok. 没关系。 Now I get the resource https://myserver.com/api/v1/eyeExams/13/contact and Spring replies: 现在我得到资源https://myserver.com/api/v1/eyeExams/13/contact和Spring回复:

{
  "sid" : "4c2ba300-477e-11e9-898e-9d6f4f2f5990",
  "createdBy" : "system",
  "createdDate" : "2018-11-01T09:00:00Z",
  "lastModifiedDate" : null,
  "lastModifiedBy" : null,
  "createdByName" : "System",
  "lastModifiedByName" : null,
  "type" : "CUSTOMER",
  "personType" : "NATURAL_PERSON",
  "firstName" : "John",
  "lastName" : "Smith",
  "companyName" : null,  
  "_links" : {
    "self" : {
      "href" : "https://myserver.com/api/v1/contact/22352"
    },
    "contact" : {
      "href" : "https://myserver.com/api/v1/contact/22352{?projection}",
      "templated" : true
    },
    "notes" : {
      "href" : "https://myserver.com/api/v1/contacts/22352/notes"
    },
    "auditLogs" : {
      "href" : "https://myserver.com/api/v1/contacts/22352/auditLogs"
    },
    "media" : {
      "href" : "https://myserver.com/api/v1/contacts/22352/media"
    },
    "privacyAgreements" : {
      "href" : "https://myserver.com/api/v1/contacts/22352/privacyAgreements"
    },
    "eyeExams" : {
      "href" : "https://myserver.com/api/v1/contacts/22352/eyeExams"
    },
    "eyeExamsCount" : {
      "href" : "https://myserver.com/api/v1/contacts/22352/eyeExams/count"
    },
    "documents" : {
      "href" : "https://myserver.com/api/v1/contacts/22352/documents"
    },
    "pendingSalesOrders" : {
      "href" : "https://myserver.com/api/v1/contacts/22352/pendingSalesOrders"
    },
    "lastPurchasedFrames" : {
      "href" : "https://myserver.com/api/v1/contacts/22352/lastPurchasedFrames"
    },
    "store" : {
      "href" : "https://myserver.com/api/v1/contact/22352/store{?projection}",
      "templated" : true
    }
  }
}

I want point out the self link. 我想指出自我链接。 It is wrong , in fact it should be https://myserver.com/api/v1/contacts/22352 with the end -s. 这是错误的 ,事实上它应该是https://myserver.com/api/v1/contacts/22352的结尾。

I was using some custom ResourceProcessor, but even without them I've the same problem. 我使用的是一些自定义ResourceProcessor,但即使没有它们我也有同样的问题。

Right now I created a workaround in ContactResourceProcessor, rewriting the self link with the right one but I'd like to understand if I'm doing something wrong, if it's a bug or I just missed something. 现在我在ContactResourceProcessor中创建了一个解决方法,用正确的方法重写自我链接,但是我想知道我做错了什么,如果它是一个错误或者我错过了什么。

It's probably a bug, but try this workaround: 这可能是一个错误,但尝试这个解决方法:

@RepositoryRestResource(path="contacts")
@Transactional
@PreAuthorize("isAuthenticated()")
public interface ContactRepository extends JpaRepository<Contact, Long> {
 ....
 ....
}

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

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