您可以通过定义一个实现 Repository 的中间通用接口来实现这一点,并公开例如所有使用
注释的 PagingAndSortingRepository 方法
@RestController(exported = false).
借助该来源:https://spring.io/blog/2011/07/27/fine-tuning-spring-data-repositories/,这是我的解决方案:
首先,将 RepositoryDetectionStrategy 设置为 ANNOTATED,以便公开的唯一存储库是那些带注释的 @RepositoryRestResource。这可以通过:
@Component
public class SpringRestConfiguration extends
RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setRepositoryDetectionStrategy(RepositoryDetectionStrategy.RepositoryDetectionStrategies.ANNOTATED);
}
}
定义您的通用 Rest 存储库。它必须只实现 Repository 接口,它是空的,而不是 CrudRepository 或 PagingAndSortingRepository,因此您可以准确控制将公开哪些方法,并且公开的方法不取决于您正在使用或将使用的 Spring Data 版本.
要保证非公开,您必须使用 @RestResource(exported=false) 注释每个方法。这有点烦人,但一劳永逸(你可以复制粘贴,我采用 CrudRepository 和 PagingAndSorting 中定义的所有 te 方法):
@RepositoryRestResource
@NoRepositoryBean
public interface RestRepositoryMethodExportedFalse<T, ID extends Serializable>
extends Repository<T, ID> {
/**
* Returns all entities sorted by the given options.
*
* @param sort
* @return all entities sorted by the given options
*/
@RestResource(exported = false)
Iterable<T> findAll(Sort sort);
/**
* Returns a {@link Page} of entities meeting the paging restriction
* provided in the {@code Pageable} object.
*
* @param pageable
* @return a page of entities
*/
@RestResource(exported = false)
Page<T> findAll(Pageable pageable);
/**
* Saves a given entity. Use the returned instance for further operations as
* the save operation might have changed the entity instance completely.
*
* @param entity
* @return the saved entity
*/
@RestResource(exported = false)
<S extends T> S save(S entity);
/**
* Saves all given entities.
*
* @param entities
* @return the saved entities
* @throws IllegalArgumentException
* in case the given entity is {@literal null}.
*/
@RestResource(exported = false)
<S extends T> Iterable<S> save(Iterable<S> entities);
/**
* Retrieves an entity by its id.
*
* @param id
* must not be {@literal null}.
* @return the entity with the given id or {@literal null} if none found
* @throws IllegalArgumentException
* if {@code id} is {@literal null}
*/
@RestResource(exported = false)
T findOne(ID id);
/**
* Returns whether an entity with the given id exists.
*
* @param id
* must not be {@literal null}.
* @return true if an entity with the given id exists, {@literal false}
* otherwise
* @throws IllegalArgumentException
* if {@code id} is {@literal null}
*/
@RestResource(exported = false)
boolean exists(ID id);
/**
* Returns all instances of the type.
*
* @return all entities
*/
@RestResource(exported = false)
Iterable<T> findAll();
/**
* Returns all instances of the type with the given IDs.
*
* @param ids
* @return
*/
@RestResource(exported = false)
Iterable<T> findAll(Iterable<ID> ids);
/**
* Returns the number of entities available.
*
* @return the number of entities
*/
@RestResource(exported = false)
long count();
/**
* Deletes the entity with the given id.
*
* @param id
* must not be {@literal null}.
* @throws IllegalArgumentException
* in case the given {@code id} is {@literal null}
*/
@RestResource(exported = false)
void delete(ID id);
/**
* Deletes a given entity.
*
* @param entity
* @throws IllegalArgumentException
* in case the given entity is {@literal null}.
*/
@RestResource(exported = false)
void delete(T entity);
/**
* Deletes the given entities.
*
* @param entities
* @throws IllegalArgumentException
* in case the given {@link Iterable} is {@literal null}.
*/
@RestResource(exported = false)
void delete(Iterable<? extends T> entities);
/**
* Deletes all entities managed by the repository.
*/
@RestResource(exported = false)
void deleteAll();
}
然后,只需在最终存储库中扩展您的自定义中间存储库,并使用您的示例唯一地覆盖您要公开的方法(您将获得自动完成,因此可以快速完成):
@RestResource(path="questions")
public interface QuestionRepository extends RestRepositoryMethodExportedFalse<Question,Long>{
/**
* Here is the only method I expose
*/
@RestResource(exported = true)
@Override
Question findOne(Long id);
}
最好使用将默认值设置为 false 的参数,但在此之前,这是我找到的唯一安全方法。