当前位置: 首页 > 面试题库 >

如何在Rest资源Spring Data Rest中将Export的默认值设置为false

公羊曜灿
2023-03-14
问题内容

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

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

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

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

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


问题答案:

您可以通过定义一个实现Repository的中间通用接口并公开(例如)所有带有PatchAndSortingRepository方法的方法来实现此目的。

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

定义您的通用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();

}

然后,只需在您的最终存储库中扩展自定义的中间存储库,并通过html" target="_blank">示例唯一覆盖要公开的方法(您会自动完成,因此可以快速完成):

@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的参数,但是在可行之前,这是我找到的唯一安全方法。



 类似资料:
  • 我是spring boot的新手,正在学习

  • 问题内容: 有没有办法为通用模板指定默认类型? 假设我有一堂课。猴子可以生活在不同的,例如或: 有没有一种方法可以为指定默认值,所以我可以使用类型而不是没有得到编译器警告? 编辑 换句话说,是否有一种方法可以摆脱“原始类型”警告而不必明确地禁止它呢? 问题答案: 不,你不能那样做。通用参数没有默认值。您可以重新组织类型层次结构,以便有一个GenericMonkey和一个DefaultMonkey可

  • 问题内容: 尝试使用WTForms设置SelectField的默认值时,我像这样将值传递给“默认”参数。 我也尝试了以下方法。 都不将默认选定字段设置为“ Def”。这适用于其他类型的字段,例如TextField。如何设置SelectField的默认值? 问题答案: 你发布的第一种方法是正确的,并且对我有用。唯一无法解释的原因可能是你正在运行旧版本的WTForms,它在1.0.1上对我有效

  • 我试图设置可变投掷SpEL在Spring启动应用程序: 应该来自应用程序-{profile}。财产。但是字段始终为空,即使已存在。 带有的代码可以正常工作,但包含空字符串。 我的问题是,如果使用SpEL的属性不存在,如何将null设置为variable。 upd:值来自特定于配置文件的属性文件

  • 问题内容: 我在使用react-select时遇到问题。我使用redux表单,并且使我的react-select组件与redux表单兼容。这是代码: 这里是我如何渲染它: 但是问题是我的下拉菜单没有我希望的默认值。我做错了什么?有任何想法吗? 问题答案: 我猜你需要这样的东西:

  • 问题内容: 我可以在angularjs中使用默认值设置一个下拉列表, 如何使用来达到相同的效果?我胆战心惊, 但是没用。样品小提琴在这里 问题答案: 使用 为设定的默认值。 这是: 演示