I have a resource that is accessed via a custom resource URI. I have requirement that primary keys cannot be directly shown in a resource URI. So I modified my REST config to provided a modified EntityLookup.
Appropriate snippets are as follows...
Modified Configuration:
@Component
public class SpringDataMyDataRestCustomization extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
/**
* Setup hashed ID URIs
*/
config.withEntityLookup().
forRepository(MyDataRepository.class, MyData::getIdHash, IMIncidentRepository::findByIdHash);
}
}
Entity class (You can see here that I am using a virtual column to create a hash of the primary key):
@Entity
class MyData {
@Id
@Column(name = "MyDataKey")
public Long getMyDataKey() {
return myDataKey;
}
public void setMyDataKey(Long myDataKey) {
this.myDataKey= myDataKey;
}
@Formula("CONVERT(VARCHAR(32), HashBytes('MD5', LTRIM(myDataKey)), 2)")
public String getIdHash() {
return DigestUtils.md5Hex(myDataKey.toString());
}
public void setIdHash(String idHash) {
this.idHash = idHash;
}
....
}
Repository:
public interface MyDataRepository extends PagingAndSortingRepository<MyData, Long> {
Optional<MyData> findByIdHash(@Param("idHash") String idHash);
}
This all works great for GET request. I am able to get a URI with the hashed primary key and retrieve resources just fine. However, when I want to add or update a resource it fails with this exception:
Failed to convert from type [java.lang.String] to type [java.lang.Long]
for value '09d8d1d6e2538a02bac5fe033e2af92e'; nested exception is
java.lang.NumberFormatException: For input string: \"09d8d1d6e2538a02bac5fe033e2af92e\"
So, it looks like it is trying to take the hashed ID as the primary key and set it on my entity. This, of course, fails since it can't cast that String to a Long.
Do I have something setup incorrectly? Any thoughts on how I could do this differently?
EDIT: Upon further review this seems to be a regression with Spring Boot 2.0. I downgraded to 1.5.10 and things started working...
Works:
- Spring Boot 1.5.10
- Spring Data Rest 2.6.10
Doesn't Work:
- Spring Boot 2.0.0
- Spring Data Rest 3.0.5
Not sure where the exact issue might be...