olivergierke #342 - Expanded copyright headers to 2018.
Removed trailing whitespace in touched files.
Latest commit d854c6d Feb 22, 2018


Spring Data REST URI customizations

This example shows how to customize which property of the domain type shall be used to create URIs for item resources.

When using Java 8, this is as easy as registering the mapping operations on Spring Data RESTs RepositoryRestConfiguration:

public class SpringDataRestCustomization extends RepositoryRestConfigurerAdapter {

  public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

      forRepository(UserRepository.class, User::getUsername, UserRepository::findByUsername);

As you can see the EntityLookupRegistrar obtained via RepositoryrestConfiguration.withCustomEntityLookup() takes two method references. The first one defines the identifier mapping, the second one defines how to look up the entity using the repository. The customization could also be defined in a slightly more explicit way like this:


In non-Java 8 environments the method references would have to be replaced with a quite verbose anonymous inner class, so that it’s probably easier to implement EntityLookup explixitly and declare it as Spring bean:

@RequiredArgsConstructor(onConstructor = @__(@Autowired) )
public class UserEntityLookup extends EntityLookupSupport<User> {

  private final @NonNull UserRepository repository;

  public Serializable getResourceIdentifier(User entity) {
     return entity.getUsername();

  public Object lookupEntity(Serializable id) {
    return repository.findByUsername(id.toString());

As you can see the customization consists of two methods that need to be symmetric in their functionality. getResourceIdentifier(…) returns the property that’s supposed to be used in the URI while lookupEntity(…) uses the value to lookup an entity via a query method for exactly that property.