我使用 Spring Data JPA 来管理 JPA 实体,Spring Data Rest 将存储库导出到 REST API。
我还在构建一个自定义控制器,我想在其中获取实体的 URI(HAL 链接)并通过CrudRepository
.
自定义控制器
@RequestMapping(path = MeLinks.ELEVATE, method = RequestMethod.PUT, consumes = RestMediaTypes.TEXT_URI_LIST_VALUE)
HttpEntity<?> elevate(@RequestBody @Valid CollectionModel<Contact> contact) {
...
}
Run Code Online (Sandbox Code Playgroud)
当尝试使用 Content-Type 之PUT
类的联系链接时,我可以使用而不是 Contact 对象本身访问链接。http://localhost:8080/contacts/1
text/uri-list
contact.getLinks()
Spring Data Rest 是否可以从 URI 自动推断实体?我知道一个内置的,UriToEntityConverter
但我如何使用它?
编辑
这是一种有效但并没有真正优雅地解决问题的尝试。
下面的代码初始化 aUriToEntityConverter
并将传入的 URI 转换为域对象。
@Autowired
private ApplicationContext applicationContext;
@Autowired
private MappingContext<?, ?> mappingContext;
@RequestMapping(path = MeLinks.ELEVATE, method = RequestMethod.PUT, consumes = RestMediaTypes.TEXT_URI_LIST_VALUE)
HttpEntity<?> …
Run Code Online (Sandbox Code Playgroud) 我正在尝试findAll()
使用自定义@Query
注释来注释 Spring Data Repository方法。我想在hasPermission()
里面使用 Spring Security表达式where
子句中。
我已经初始化了SecurityEvaluationContextExtension
bean 以允许在 SpEL 中使用常见的内置安全表达式。
@Bean
public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
return new SecurityEvaluationContextExtension();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用自定义PermissionEvaluator
实现。
@Query("select t from #{#entityName} t where 1=?#{hasPermission(filterObject, 'read') ? 1 : 0}")
Page<Stream> findAll(Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
我得到以下异常。hasPermission
由于某种原因,它似乎无法访问。hasRole
工作得很好。
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'filterObject' cannot be found on object of type 'java.lang.Object[]' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:226)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
at org.springframework.expression.spel.ast.MethodReference.getArguments(MethodReference.java:155) …
Run Code Online (Sandbox Code Playgroud)