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;
}
1条答案
按热度按时间5w9g7ksd1#
发现了问题。为了使findAll()起作用,模型必须具有已删除状态。
我已经更新了模型如下