I am developing an API using Spring Boot and use Spring Data REST to handle most of the CRUD API heavy lifting. For our resources, we avoid exposing their internal IDs to the end user, using slugs instead to identify resources on the server side.
In this example, the resource is uniquely identifier by two slugs: the category-slug and the item-slug, with an URL like this: /items/category-slug/item-slug.
However, Spring Data REST relies on a single identifier. I have written a configurer to map the category-slug to a category like so:
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
// Categories are exposed by slug, this is a simple 1:1 conversion.
config.withEntityLookup().forRepository(CategoryRepository.class, Category::getSlug, CategoryRepository::findBySlug);
}
However, how can I do this using two identifiers? Below is what I'd like to do but cannot:
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
config.withEntityLookup().forRepository(
ItemRepository.class,
item -> String.format("%s/%s", item.getCategory.getSlug(), item.getSlug()),
ItemRepository::findBySlugAndCategorySlug);
}