我有一个实体对象,我使用spring data mongodb在mongodb中坚持使用,并将REST API暴露于查询。实体看起来如下:
entity.java
@Document
public class Entity {
@Id
String id;
Map<String, String> properties;
}
相应的弹簧数据存储库如下。它利用弹簧数据mongoDB与querydsl的集成:
entityrepository.java
public interface EntityRepository extends MongoRepository<Entity, String>
,QueryDslPredicateExecutor<Entity> {
}
这是我查询存储的实体的控制器:
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);
}
}
当我击中URL localhost:8080/entities/q?id=xxx
时,它可以按预期工作。但是,当我击中url localhost:8080/entities/q?properties.p1=v1
(查询密钥和 Map
属性的值)时,我在方法中得到了一个null谓词,因此所有实体都在返回。
我该如何使其正常工作?
您应该将properties
字段存储为key-value
的List
,然后可以在其中搜索。
您的查询应与下一个结构一起使用:
{ id : "1233454",
properties : [
{ "key : "key", "value" : "value" }
]
}