首页 > 解决方案 > GAE - 没有为此线程注册 API 环境

问题描述

我有一个使用谷歌应用引擎部署到谷歌云的 spring-boot 应用程序。有一个包含多个实体的数据存储。当我尝试通过我的应用程序检索数据时(应用程序已上传到 appengine,而不是本地),它总是会引发错误 -

出现意外错误(类型=内部服务器错误,状态=500)。没有为此线程注册 API 环境。

我试图让它以这种方式工作:

@Bean
public FirebaseAuth firebaseAuth() throws IOException {
    ClassLoader classLoader = this.getClass().getClassLoader();
    InputStream
        serviceAccount = classLoader.getResourceAsStream("downloaded-key-270620-2a16da8f4062.json");
    FirebaseOptions options = (new Builder())
                                  .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                                  .setDatabaseUrl(
                                      "https://my-app.firebaseio.com")
                                  .build();
    FirebaseApp.initializeApp(options);
    return FirebaseAuth.getInstance();
}

private Result<Product> list(boolean wholesale, String startCursorString) {
    FetchOptions fetchOptions = Builder.withLimit(10);
    if (startCursorString != null && !startCursorString.equals("")) {
        fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString));
    }

    Query query = (new Query("Product")).setFilter(
        new FilterPredicate("wholesale", FilterOperator.EQUAL, wholesale))
                                        .addSort("name", SortDirection.ASCENDING);
    PreparedQuery preparedQuery = this.datastore.prepare(query);
    QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);
    List<Product> resultBooks = this.entitiesToObjects(results);
    Cursor cursor = results.getCursor();
    if (cursor != null && resultBooks.size() == 10) {
        String cursorString = cursor.toWebSafeString();
        return new Result(resultBooks, cursorString);
    } else {
        return new Result(resultBooks);
    }
}

public void configure(WebSecurity web) throws Exception {
    web.ignoring()
       .antMatchers(HttpMethod.GET, new String[] { "/products" })
       .antMatchers(HttpMethod.GET, new String[] { "/purchases/update" })
       .antMatchers(HttpMethod.GET, new String[] { "/purchases/statistics/**" });
}

知道可能是什么问题吗?

标签: firebasegoogle-app-enginegoogle-cloud-firestoregoogle-cloud-datastore

解决方案


我认为您可能会根据文档缺少初始化它,它类似于:

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

推荐阅读