#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:
Oliver Gierke
2015-12-09 16:10:02 +01:00
parent 0de282b2f2
commit fe6e2172cd
3 changed files with 84 additions and 13 deletions

View File

@@ -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);
}
}

View File

@@ -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> {