java Springboot MongoRepository findAll()方法返回空列表,但其他方法返回有效结果

a64a0gku  于 2023-05-05  发布在  Java
关注(0)|答案(1)|浏览(227)

mongoRepository的findAll()方法返回一个空列表。下面的代码有什么问题?用于计算集合中文档数量的API工作正常,因此findAll()也应该返回有效的结果。
控制器

@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();
    }
}

服务

@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();
    }
}

仓库

@Repository
public interface AuthScopeRepository extends MongoRepository<AuthScope, String> {
    Optional<AuthScope> findByScope(String id);
}

模型如下

@Data
@Document("auth-scopes")
public class AuthScope  {
    @Id
    private String scope;
    private String belongsToApi;
    private String belongsToApiTitle;
    private String description;
}
5w9g7ksd

5w9g7ksd1#

发现了问题。为了使findAll()起作用,模型必须具有已删除状态。
我已经更新了模型如下

@Data
@Document("auth-scopes")
public class AuthScope  {
    @Id
    private String scope;
    private String belongsToApi;
    private String belongsToApiTitle;
    private String description;
    private boolean deleted;

}

相关问题