2017-05-31 3 views
0

私は春データ休憩のRestResourceアノテーションを使用したいと思います。あなたが知っているように、デフォルトでALL CRUDメソッドを公開します。しかし、私はfindAllメソッドだけが必要です。一つの方法は、このような偽の他のすべてのメソッドのエクスポート値を設定することです:休憩リソースの春データ休憩で偽としてエクスポートのデフォルト値を設定する方法

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

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

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

をしかし、私はこれが好きではありません。私はこの冶金を避けることができるので、他の簡単な方法はありますか?

@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

答えて

1

あなたがリポジトリを実装して、中間の一般的なインタフェースを定義することによって、これを達成するため、例えば、公開することができますが、すべての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); 
} 
} 

一般的なレストレポジトリを定義します。 CrudRepositoryやPagingAndSortingRepositoryではなく、空のリポジトリインタフェースのみを実装する必要があるため、どのメソッドが公開されるかを正確に制御でき、公開されているメソッドは使用しているSpringデータバージョンに依存しません。

非展示を保証するには、各メソッドに@RestResource(exported = false)とアノテーションを付ける必要があります。 、そして、

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

} 

だけで、最終的なリポジトリにカスタム中間リポジトリを拡張し、:それは(あなただけのコピー&ペーストをすることができ、私はテ方法がCrudRepositoryとPagingAndSortingで定義するすべて取る)ビット迷惑以外のすべてのために一度行われますこれがあるまで好まれますfalseにエクスポートのデフォルト値を設定する

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

} 

パラメータを、しかし:あなたはあなたの例で、公開するユニークなメソッドをオーバーライド(あなたは、それが迅速に行われますので、自動補完を取得します)可能な限り、私が見つける唯一の安全な方法です。

0

次の例のように、findAll方法の結果だけを返しますGET /questions要求のためのカスタムコントローラを実装する必要があります。

関連する問題