0

I'm exposing my Entity classes with spring-data-rest and spring-data-jpa without difining any custom controllers.

@Entity
public class Domain {

    @Id
    private String domainName;

    Domain(String name) {
      this.domainName = name;
    }

    private Domain() { }
}

Say I want to create a Domain with id stackoverflow.com, what should I pass in the request body?

Say the Domain resource is exposed at the endpoint /domains. The reason I'm setting @Id to an String is that I want to be able to retrive a domain by GET /domains/{domainName}

Update

When sending a POST request as follows:

POST /domains
{
    "domainName": "stackoverflow.com"
}

I get the exception:

JpaSystemException: ids for this class must be manually assigned before calling save(): space.linuxdeveloper.Network; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): space.linuxdeveloper.Network] with root cause

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
  • With no public no args constructor you will most likely need to use Jackson's `@JsonCreator` annotation to handle deserialization. http://www.cowtowncoder.com/blog/archives/2011/07/entry_457.html and https://stackoverflow.com/questions/21920367/why-when-a-constructor-is-annotated-with-jsoncreator-its-arguments-must-be-ann – Alan Hay Nov 20 '17 at 08:37

1 Answers1

0

Try to use this request body:

POST /domains
{
    "domainName": "stackoverflow.com"
}

Don't forget to correct your c-tor:

Domain(String domainName) {
  this.domainName = domainName;
}
Cepr0
  • 28,144
  • 8
  • 75
  • 101