[英]Spring Data REST, QueryDSL and HashMaps - Null Predicate from URL when referring to HashMap properties
I have an entity object that I'm persisting in MongoDB using Spring Data MongoDB and exposing a REST API to query it. 我有一个实体对象,我使用Spring Data MongoDB在MongoDB中持久化,并公开REST API来查询它。 The Entity looks like the following :
实体如下所示:
Entity.java Entity.java
@Document
public class Entity {
@Id
String id;
Map<String, String> properties;
}
Corresponding Spring Data repository is as below. 对应的Spring Data存储库如下。 It leverages Spring Data MongoDB's integration with QueryDsl:
它利用Spring Data MongoDB与QueryDsl的集成:
EntityRepository.java EntityRepository.java
public interface EntityRepository extends MongoRepository<Entity, String>
,QueryDslPredicateExecutor<Entity> {
}
Here's the controller through which I query the stored Entities: 这是我通过它查询存储的实体的控制器:
EntityController.java EntityController.java
@RestController
@RequestMapping(value = "/entities")
public class EntityController {
private static final Logger logger = LoggerFactory.getLogger(EntityController.class);
@Autowired
EntityRepository entityRepository;
@RequestMapping(method = RequestMethod.GET)
public Page<Entity> findAllEntities(Pageable pageable) throws ResourceQueryException {
return entityRepository.findAll(pageable);
}
@RequestMapping(method = RequestMethod.GET, value = "/q")
public Page<Entity> filterEntities(
@QuerydslPredicate(root = Entity.class) Predicate predicate,
Pageable pageable) {
return entityRepository.findAll(predicate,pageable);
}
}
When I hit the url localhost:8080/entities/q?id=xxx
it works as expected. 当我点击url
localhost:8080/entities/q?id=xxx
它按预期工作。 However, when I hit the url localhost:8080/entities/q?properties.p1=v1
(query keys and values of the Map
property), I get a null Predicate in my method, due to which all Entities are being returned. 但是,当我点击url
localhost:8080/entities/q?properties.p1=v1
(查询键和Map
属性的值)时,我在我的方法中得到一个null谓词,因此返回所有实体。
How do I get this to work properly? 如何让它正常工作?
You should store your properties
field as List
of key-value
pairs then it would be possible to search in it. 您应该将
properties
字段存储为key-value
对List
,然后就可以在其中进行搜索。
Your query should work with next structure: 您的查询应该适用于下一个结构:
{ id : "1233454",
properties : [
{ "key : "key", "value" : "value" }
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.