您可以通過定義一個實現倉庫的中間通用接口實現這一目標,並揭露,例如,所有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);
}
}
定義您的通用休息存儲庫。它必須只實現Repository接口,它是空的,而不是CrudRepository或PagingAndSortingRepository,所以你可以精確地控制哪些方法將被公開,並且公開的方法不依賴於你正在使用的Spring Data版本,或者將使用。
爲了保證非展覽,你必須使用@RestResource(exported = false)註解每種方法。這是一個有點麻煩,但對於所有做一次(你可以複製粘貼,我採取一切TE方法CrudRepository定義和PagingAndSorting):
@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會中將優先的默認值,但直到這可能的,這裏是我找到的唯一安全的方式。