0

I have two domain objects in my project, Document and Project. Documents are associated with a single Project each. I'm using Spring-Data-Rest and it's Repository abstraction, so I have this:

@Entity
public class Project extends AbstractLongIdEntity {

    @Column(length=50,nullable=false)
    private String title;

    @Column(length=200,nullable=true)
    private String description;

...

}


@Entity
public class Document extends AbstractLongIdEntity {

    @Column(length=50,nullable=false)
    private String title = "New Diagram";

    @Column(length=200,nullable=true)
    private String description;

    public Document() {
    }

    public Document(String title, String description, Project project) {
        super();
        this.title = title;
        this.description = description;
        this.project = project;
    }

    @ManyToOne(targetEntity=Project.class, optional=false)
    private Project project;

...
}

When I GET a Document over HTTP, I get this back:

[DEBUG] TEST - Response: 201 {
  "title" : "My Document",
  "description" : "Some name for a document",
  "dateCreated" : "2018-02-13T06:33:14.397+0000",
  "lastUpdated" : null,
  "internalId" : 1,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/documents/1"
    },
    "document" : {
      "href" : "http://localhost:8080/api/documents/1"
    },
    "currentRevision" : {
      "href" : "http://localhost:8080/api/documents/1/currentRevision"
    },
    "project" : {
      "href" : "http://localhost:8080/api/documents/1/project"
    }
  }
}

However, I can't POST the same content back to create the Document in the first place. The best I've found is that I can POST this:

{
  "title" : "My Document",
  "description" : "Some name for a document",
  "project" : "http://localhost:8080/api/projects/1",
  "currentRevision" : null,
  "dateCreated" : 1518503594397,
  "lastUpdated" : null
}

However, this seems really weird, as:

a) I now have an unnamed, untyped link embedded within the object, and this isn't very HATEAOS (although Spring seems to deserialise it correctly).

b) I now have to create a separate class, DocumentResource looking like this:

public class DocumentResource extends ResourceSupport {

    public String title;
    public String description;
    public String project;

...
}

In which, Project is a String. Which is a pain as I now essentially have two very similar domain objects.

So the question: What is the right way to POST new entities in HATEOAS / Spring Data Rest and have them create relationships in the database?

I am using Spring Boot 1.5.10, which seems to bring in Spring-Data-Rest 2.6.10 and Spring-Hateoas 0.23.0.

thanks

Rob Moffat
  • 465
  • 5
  • 11

1 Answers1

0

You can try posting like this:

{
  "title" : "My Document",
  "description" : "Some name for a document",
  "project" : "/projects/1",
  "currentRevision" : null,
  "dateCreated" : 1518503594397,
  "lastUpdated" : null
}
  • Hi Paul, yes, I mention this approach, and why it’s not great in the question. It works, kinda, but it’s not what I’m after – Rob Moffat Feb 13 '18 at 18:06