From fe6e2172cdb398b06c1e771b4553e8211c5256d8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 9 Dec 2015 16:10:02 +0100 Subject: [PATCH] #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. --- rest/uri-customization/readme.adoc | 50 +++++++++++++++---- .../uris/SpringDataRestCustomization.java | 40 +++++++++++++++ .../rest/uris/UserEntityLookup.java | 7 +-- 3 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 rest/uri-customization/src/main/java/example/springdata/rest/uris/SpringDataRestCustomization.java diff --git a/rest/uri-customization/readme.adoc b/rest/uri-customization/readme.adoc index 7c08e1c7..721ee805 100644 --- a/rest/uri-customization/readme.adoc +++ b/rest/uri-customization/readme.adoc @@ -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 { - 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()); + } } ---- diff --git a/rest/uri-customization/src/main/java/example/springdata/rest/uris/SpringDataRestCustomization.java b/rest/uri-customization/src/main/java/example/springdata/rest/uris/SpringDataRestCustomization.java new file mode 100644 index 00000000..e512e8e7 --- /dev/null +++ b/rest/uri-customization/src/main/java/example/springdata/rest/uris/SpringDataRestCustomization.java @@ -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); + } +} diff --git a/rest/uri-customization/src/main/java/example/springdata/rest/uris/UserEntityLookup.java b/rest/uri-customization/src/main/java/example/springdata/rest/uris/UserEntityLookup.java index f30f0cc9..2985f7f4 100644 --- a/rest/uri-customization/src/main/java/example/springdata/rest/uris/UserEntityLookup.java +++ b/rest/uri-customization/src/main/java/example/springdata/rest/uris/UserEntityLookup.java @@ -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 {