#144 - Make use of the dedicated configuration API for EntityLookups.
Rather than declaring a dedicated EntityLookup bean instance we now use the configuration API introduced on RepositoryRestConfiguration to define the identifier and lookup mapping. Tweaked readme accordingly.
This commit is contained in:
@@ -1,6 +1,36 @@
|
||||
= 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. This is achieved by implementing an `EntityLookup` and declaring it as Spring bean:
|
||||
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`:
|
||||
|
||||
[source, java]
|
||||
----
|
||||
@Component
|
||||
public class SpringDataRestCustomization extends RepositoryRestConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
|
||||
|
||||
config.withCustomEntityLookup().//
|
||||
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:
|
||||
|
||||
[source, java]
|
||||
----
|
||||
config.withCustomEntityLookup().
|
||||
forRepository(UserRepository.class).
|
||||
withIdMapping(User::getUsername).
|
||||
withLookup(UserRepository::findByUsername);
|
||||
----
|
||||
|
||||
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:
|
||||
|
||||
[source, java]
|
||||
----
|
||||
@@ -8,17 +38,17 @@ This example shows how to customize which property of the domain type shall be u
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired) )
|
||||
public class UserEntityLookup extends EntityLookupSupport<User> {
|
||||
|
||||
private final @NonNull UserRepository repository;
|
||||
private final @NonNull UserRepository repository;
|
||||
|
||||
@Override
|
||||
public Serializable getResourceIdentifier(User entity) {
|
||||
return entity.getUsername();
|
||||
}
|
||||
@Override
|
||||
public Serializable getResourceIdentifier(User entity) {
|
||||
return entity.getUsername();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object lookupEntity(Serializable id) {
|
||||
return repository.findByUsername(id.toString());
|
||||
}
|
||||
@Override
|
||||
public Object lookupEntity(Serializable id) {
|
||||
return repository.findByUsername(id.toString());
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.rest.uris;
|
||||
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Spring Data {@link RepositoryRestConfiguration} to customize the identifier mapping for {@link User}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Component
|
||||
public class SpringDataRestCustomization extends RepositoryRestConfigurerAdapter {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter#configureRepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration)
|
||||
*/
|
||||
@Override
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
|
||||
|
||||
config.withCustomEntityLookup().//
|
||||
forRepository(UserRepository.class, User::getUsername, UserRepository::findByUsername);
|
||||
}
|
||||
}
|
||||
@@ -23,15 +23,16 @@ import java.io.Serializable;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.rest.core.support.EntityLookup;
|
||||
import org.springframework.data.rest.core.support.EntityLookupSupport;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Custom {@link EntityLookup} to replace the usage of the database identifier in item resource URIs with the username
|
||||
* property of the {@link User}.
|
||||
* property of the {@link User}. This one is not really used out of the box as it's not a Spring bean. It's just a
|
||||
* sample of how to deploy a customization in non-Java 8 environments which can't use the fluent API in use in
|
||||
* {@link SpringDataRestCustomization}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @see SpringDataRestCustomization#configureRepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration)
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired) )
|
||||
public class UserEntityLookup extends EntityLookupSupport<User> {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user