在 CosmosDB 注释中指定时找不到符号 - 方法 connectionStringSetting()

问题描述 投票:0回答:1

我正在尝试用 Java 创建一个非常基本的 Azure Function HTTP 触发器,当进行 API 调用时,它将向 CosmosDB 写入一条记录;但是,当我尝试在

connectionStringSetting
注释中指定
@CosmosDBOutput
时,出现以下错误。请注意,我只是尝试在本地进行测试,因此尚未直接在 Azure 中创建数据库、集合等。

我的

@CosmosDBOutput
注释看起来像这样:

@CosmosDBOutput(name = "document", databaseName = "YourDatabaseName", containerName = "myContainer", connectionStringSetting="myConnectionString", connection = "CosmosDBConnection") OutputBinding<Subscriber> subscriber, final ExecutionContext context)

我的

local.settings.json
看起来像这样:

 {
   "IsEncrypted": false,
   "Values": {
     "AzureWebJobsStorage": "UseDevelopmentStorage=true",
     "FUNCTIONS_WORKER_RUNTIME": "java",
     "CosmosDBConnection": "YOUR_COSMOS_DB_CONNECTION_STRING",
     "connectionStringSetting": "myConnectionString"
   }
 }

我尝试了不同的方法,例如从 local.settings.json 中删除 connectionStringSetting 并仅在注释中指定,反之亦然,将所有内容一起删除,但似乎没有任何效果。当我将鼠标悬停在 IntelliJ 中的红色波浪线上时,它告诉我它“无法解析我的注释上的方法 connectionStringSetting”。非常感谢任何帮助。

Java Version: 21
azure-functions-java-library version: 3.0.0
azure-functions-maven-plugin: 1.23.0
azure azure-functions azure-web-app-service azure-cosmosdb
1个回答
0
投票

我尝试按照

MS Docs
在我的代码中使用connectionStringSetting(),我也遇到了同样的错误,

Compilation failure: Compilation failure:
[ERROR] */javafunc/src/main/java/com/function/Function.java:[30,17] cannot find symbol
[ERROR]   symbol:   method connectionStringSetting()
[ERROR]   location: @interface com.microsoft.azure.functions.annotation.CosmosDBOutput
[ERROR] */javafunc/src/main/java/com/function/Function.java:[26,13] annotation @com.microsoft.azure.functions.annotation.CosmosDBOutput is missing a default value for the element 'connection'

enter image description here

我用

connection
代替
connectionStringSetting
,它对我有用。

微软文档需要更正。我提出了文件更正请求

#我的代码:

package com.function;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.OutputBinding;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.CosmosDBOutput;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

import java.util.Optional;
import java.util.Random;

public class Function {

    @FunctionName("HttpExample")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET, HttpMethod.POST},
                authLevel = AuthorizationLevel.ANONYMOUS)
                HttpRequestMessage<Optional<String>> request,
            @CosmosDBOutput(
                name = "database",
                databaseName="javadb",
                containerName = "data",
                connection = "COSMOSDB_connectionstring")
                OutputBinding<String> out,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        final String query = request.getQueryParameters().get("name");
        final String name = request.getBody().orElse(query);

        final int id = Math.abs(new Random().nextInt());

        final String jsonDocument = "{\"id\":\"" + id + "\", " +
                                    "\"description\": \"" + name + "\"}";

        context.getLogger().info("Document to be saved: " + jsonDocument);

        out.setValue(jsonDocument);

        return request.createResponseBuilder(HttpStatus.OK)
        .body("Document saved successfully: "+jsonDocument)
        .build();
    }
}

OUTPUT
:

enter image description here

**

Document saved successfully: {"id":"1043435924", "description": "Vivek"}

**

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.