I can’t figure out how to use Id<>
and Ids<>
in service methods with the REST API. I keep getting hit with errors.
Ids<>
For example, with a method signature such as:
public List<SomeEntity> doSomething(Ids<SomeEntity, UUID> someEntityIds);
As an array
I try posting the IDs as an array:
{
"somEntityIds": [
"23465-junk-ID-2e57w474fdg",
"36783-junk-ID-245ugwhj436"
]
}
and then it complains that "23465-junk-ID-2e57w474fdg"
is not a JSON Object.
As an object matching the Id
class structure
I try matching the structure to Id.java
:
{
"somEntityIds": [
{
"id": "23465-junk-ID-2e57w474fdg",
"entityClass": {
"_entityName": "prefix$SomeEntity"
}
},
{
"id": "36783-junk-ID-245ugwhj436",
"entityClass": {
"_entityName": "prefix$SomeEntity"
}
}
]
}
But then it complains that SomeEntity
doesn’t contain a entityClass
property, as well as java.lang.IllegalArgumentException: argument type mismatch
.
As a list of Entity
objects
I then try to pass SomeEntity
-like objects:
{
"somEntityIds": [
{
"id": "23465-junk-ID-2e57w474fdg",
"_entityName": "prefix$SomeEntity"
},
{
"id": "36783-junk-ID-245ugwhj436",
"_entityName": "prefix$SomeEntity"
}
]
}
Which then complains about java.lang.IllegalArgumentException: argument type mismatch
without any further information.
Id<>
public SomeEntity doSomething(Id<SomeEntity, UUID> someEntityId);
As a string
{
"someEntityId": "43576-junk-ID-0245y7hbj3404"
}
If I pass just a raw ID, it complains Expected BEGIN_OBJECT but was STRING
.
As an Entity
object
{
"someEntityId": {
"id": "43576-junk-ID-0245y7hbj3404"
}
}
This works, except that later in the method when I run em.find()
using the ID, this error is thrown: java.lang.IllegalArgumentException: You have provided an instance of an incorrect PK class for this find operation. Class expected : class java.util.UUID, Class received : class java.lang.String.
.
Note: I tried with and without an _entityName
property on the same level as the id
property, and both had the same result.
My current workaround
Ids<>
Method signature:
public List<SomeEntity> doSomething(List<SomeEntity> someEntities);
Post:
{
"someEntities": [
{
"id": "23465-junk-ID-2e57w474fdg",
"_entityName": "prefix$SomeEntity"
},
{
"id": "36783-junk-ID-245ugwhj436",
"_entityName": "prefix$SomeEntity"
}
]
}
Id<>
Method signature:
public SomeEntity doSomething(UUID someEntityId);
Post:
{
"someEntityId": "43576-junk-ID-0245y7hbj3404"
}
Conclusion
I would like to use Ids<>
and Id<>
since they seem to be the more appropriate tools for the job. But how do I do it?