如何设置导出的默认值为false其余资源spring data rest

如何设置导出的默认值为false其余资源spring data rest

问题描述:

我想使用RestResource注释spring数据rest。如您所知,默认情况下会公开所有CRUD方法。但我只需要findAll方法。一种方法是将所有其他方法的导出值设置为false,如下所示:如何设置导出的默认值为false其余资源spring data rest

@RestResource(path="questions") 
public interface QuestionRepository extends CRUDRepository<Question,Long> { 

@RestResource(exported = false) 
void delete(Long id); 

@RestResource(exported = false) 
void create(Question q); 
.... 
} 

但我不喜欢这样。有没有其他更简单的方法可以避免这种冶金?

您可以通过定义一个实现仓库的中间通用接口实现这一目标,并揭露,例如,所有PagingAndSortingRepository方法标注有

@RestController(exported = false). 

与该源的帮助:https://spring.io/blog/2011/07/27/fine-tuning-spring-data-repositories/,这里是我的解决方案:

首先,将RepositoryDe​​tectionStrategy设置为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会中将优先的默认值,但直到这可能的,这里是我找到的唯一安全的方式。

你应该实施GET /questions要求定制控制器将返回findAll方法的唯一结果,例如:

@RequiredArgsConstructor 
@BasePathAwareController 
@RequestMapping("/questions") 
public class QuestionController { 

    private final @NonNull QuestionRepository repository; 
    private final @NonNull PagedResourcesAssembler<Question> assembler; 
    private final @NonNull EntityLinks links; 

    @GetMapping 
    ResponseEntity<?> get(Pageable pageable) { 
     return ResponseEntity.ok(assembler.toResource(repository.findAll(pageable), 
       (ResourceAssembler<Question, ResourceSupport>) question -> 
        new Resource<>(question, 
        links.linkToSingleResource(question).withSelfRel()))); 
    } 
} 

和禁用QuestionRepository要导出:

@RepositoryRestResource(exported = false) 
public interface QuestionRepository extends JpaRepository<Question, Long> { 
} 

工作example