首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoRepository findAll()返回空列表

MongoRepository findAll()返回空列表
EN

Stack Overflow用户
提问于 2022-08-24 13:08:14
回答 1查看 62关注 0票数 0

findAll() of mongoRepository返回空列表。下面的代码有什么问题?用于计算集合中文档数量的API运行良好。

控制器

代码语言:javascript
复制
@RestController
@RequestMapping("/api/api-management/scopes")
public class AuthScopesController {
    private final ScopesService scopesService;

    @Autowired
    AuthScopesController(ScopesService scopesService) {
        this.scopesService = scopesService;
    }

    @PostMapping("/")
    public AuthScope createScope(@RequestBody AuthScope authScope) {
        return scopesService.createAuthScope(authScope);
    }

    @GetMapping("/")
    public List<AuthScope> getAllScopes() {
        return scopesService.getAuthScopes();
    }
}

服务

代码语言:javascript
复制
@Service
public class ScopesService {

    private final AuthScopeRepository authScopeRepository;

    public ScopesService(AuthScopeRepository authScopeRepository) {
        this.authScopeRepository = authScopeRepository;
    }

    public AuthScope createAuthScope(AuthScope authScope) {
        return authScopeRepository.save(authScope);
    }
    //TODO: recheck
    public List<AuthScope> getAuthScopes() {
        return authScopeRepository.findAll();
    }
}

存储库

代码语言:javascript
复制
@Repository
public interface AuthScopeRepository extends MongoRepository<AuthScope, String> {
    Optional<AuthScope> findByScope(String id);
}

模型如下

代码语言:javascript
复制
@Data
@Document("auth-scopes")
public class AuthScope  {
    @Id
    private String scope;
    private String belongsToApi;
    private String belongsToApiTitle;
    private String description;
}
EN

回答 1

Stack Overflow用户

发布于 2022-08-25 08:10:44

发现了问题。为了使findAll()工作,模型必须具有删除状态。

我更新了模型如下

代码语言:javascript
复制
@Data
@Document("auth-scopes")
public class AuthScope  {
    @Id
    private String scope;
    private String belongsToApi;
    private String belongsToApiTitle;
    private String description;
    private boolean deleted;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73473785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档