mongo @query使用mongorepository使用spring-data的问题

时间:2018-09-14 18:32:48

标签: mongodb spring-data spring-data-mongodb

我有以下查询:

@Query(value= "{ 'agents.agentId' : ?0 }", fields = "{ '_id' : 1, 'agents' : 1}")
Optional<List<Simple>> findOptionalByAgentsAgentIdIn(List<String> agentIdList);

@Query(value= "{ 'agents.agentId' : ?0 }", fields = "{ '_id' : 1, 'agents' : 1}")
Optional<List<Simple>> findOptionalByAgentsAgentId(String agentId);

第一个每次返回一个空数组,第二个返回正确的json

@Data
public class Simple {

    String id;
    List<AppAgent> agents;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AppAgent {

    private String agentName;
    private String agentId; 
}

上面是我的Simple对象,下面是我的控制器方法

@GetMapping(value = { "/apps/{agentIds}", "/apps/{agentId}" })
public ResponseEntity<RestResult<List<Simple>>> getAllApplicationsByAgentIds(
        @PathVariable Optional<List<String>> agentIds, @PathVariable Optional<String> agentId) {

    Optional<List<Simple>> documents = Optional.empty();

    if (agentId.isPresent()) {
        documents = appStatusService.getAllByAgentId(agentId.get());
    } else if (agentIds.isPresent()) {
        documents = appStatusService.getAllByAgentIds(agentIds.get());
    }

    ResponseEntity<RestResult<List<Simple>>> response;
    if (!documents.get().isEmpty()) {
        response = new ResponseEntity<>(RestResult.success(documents.get()), HttpStatus.OK);
    } else {
        response = new ResponseEntity<>(RestResult.<List<Simple>>noResult(), HttpStatus.NOT_FOUND);
    }

    return response;
}

该服务仅调用存储库方法。我的语法有问题吗?回来说没有休息的结果,就像我的else子句中预期的那样

0 个答案:

没有答案