将Export的默认值设置为false

将Export的默认值设置为false

本文介绍了如何在Rest资源Spring Data Rest中将Export的默认值设置为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use RestResource annotation of spring data rest. As you know it exposes ALL CRUD methods by default. But I only need findAll method. One way is to set exported value of all other methods to false like this:

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

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

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

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

But I don't like this. Is there any other simpler way so I can avoid this metallurgy?

推荐答案

您可以通过定义一个实现Repository的中间通用接口并公开(例如)用

You can achieve this by defining an intermediate generic interface which implements Repository, and expose, for example, all PagingAndSortingRepository methods annotated with

@RestController(exported = false).

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

With the help of that source : https://spring.io/blog/2011/07/27/fine-tuning-spring-data-repositories/, here's my solution :

首先,将RepositoryDe​​tectionStrategy设置为ANNOTATED,这样公开的唯一存储库就是那些带有@RepositoryRestResource注释的存储库.可以通过以下方式完成:

First of all, set the RepositoryDetectionStrategy to ANNOTATED so the only repositories exposed are those annotated @RepositoryRestResource. This can be done with :

@Component
public class SpringRestConfiguration extends
RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setRepositoryDetectionStrategy(RepositoryDetectionStrategy.RepositoryDetectionStrategies.ANNOTATED);
}
}

定义通用Rest存储库.它必须仅实现为空的Repository接口,而不必实现CrudRepository或PagingAndSortingRepository,因此您可以精确地控制将公开哪些方法,并且公开的方法不取决于您正在使用或将使用的Spring Data版本.

Define your generic Rest repository. It has to implement only Repository interface, which is empty, and not CrudRepository or PagingAndSortingRepository, so you can control exactly which methods will be exposed, and the methods exposed doesn't depends on the Spring Data version you're using, or will use.

要保证非博览会,您必须使用@RestResource(exported = false)每种方法进行注释.有点烦人,但要一次完成(您可以复制粘贴,我可以使用在CrudRepository和PagingAndSorting中定义的所有te方法):

To garantee the non-exposition you have to annotate with @RestResource(exported=false) each method. It's a bit annoying but done once for all (you can just copy-paste, I take all te methods define in CrudRepository and 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();

}

然后,只需将您的自定义中间存储库扩展到最终存储库中,并使用示例(您会自动完成,以便快速完成)来唯一覆盖要公开的方法:

Then, just extends your custom intermediate repository in your final repositories, and override uniquely the method you want to expose, with your example (you get auto-completion so it's quickly done) :

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

}

最好使用将export的默认值设置为false的参数,但是在可行之前,这是我找到的唯一安全方法.

A parameter to set the default value of exported to false would be prefered, but until this is possible, here is the only safe way I find.

这篇关于如何在Rest资源Spring Data Rest中将Export的默认值设置为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:59