Question

I am using Spring Data REST, and I am trying to change a many-to-one relation using Spring REST, but I cannot get the proper http call to work.

My entity looks like this (basic calls like creation with POST etc. works fine):

{
  "id" : 70,
  "productId" : yyy,
  "productdiscount" : 10,
  "version" : 0,
  "description" : "xxx",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/rest/rules/70"
    },
    "timedefinition" : {
      "href" : "http://localhost:8080/rest/rules/70/timedefinition"
    }
  }
}

I want to change the current timedefinition with say ID 1, to ID 2.

I have tried a lot of different call types with different errors. The below call doesn´t work.

 curl -X PUT -H "Content-Type: text/uri-list" -d 'http://localhost:8080/rest/timedefinition/1' http://localhost:8080/rest/rules/70/timedefinition

The following error is received:

Failed to convert from type java.lang.String to type domain.TimeDefinition for value '0'; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class domain.TimeDefinition. Expected: class java.lang.Integer, got class java.lang.Long

Another example is this:

curl -X PUT -H "Content-Type: application/json" -d '{"timedefinition": {"href" : "http://localhost:8080/rest/timedefinition/0", "rel" : "timedefinition"} }' http://localhost:8080/rest/rules/70/timedefinition

The error I get is:

"message":"Must send only 1 link to update a property reference that isn't a List or a Map."

The main reference at http://docs.spring.io/spring-data/rest/docs/2.0.1.RELEASE/reference/html/ doesn´t give much info on the topic above unfortunetely.

Any insights and explanations on the proper REST query format to update entities assocations are greatly appreciated!

Était-ce utile?

La solution

Correct answer is as my comment:

curl -v -X PUT -H "Content-Type: text/uri-list" -d "http://localhost:8080/rest/timedefinition/0" http://localhost:8080/rest/rules/70/timedefinition

Autres conseils

There was a bug in spring-data-rest-webmvc:3.2.0-RELEASE that would throw this error every time a PUT was sent to create 1-to-1 associations.

Changing spring-boot-starter-parent from 2.2.0.RELEASE to 2.2.1.RELEASE resolved this for me. You can also manually override spring-data-rest-webmvc to 3.2.1.RELEASE.

Related commit: https://github.com/spring-projects/spring-data-rest/commit/202a8aa30221b81dece183b74d81278deaf45321

You can also use application/json content-type by embedding your relation into a _links node.

curl -X PUT -H "Content-Type: application/json" -d '{"_links":{"timedefinition": {"href" : "http://localhost:8080/rest/timedefinition/0", "rel" : "timedefinition"} }}' http://localhost:8080/rest/rules/70/timedefinition
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top