有人可以解释一下 Azure 函数参数内的接口的用途吗?

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

我正在使用 Azure 函数,我注意到

request
参数具有名为 HttpRequestMessage 的接口类型(请参阅 (1))。这个
request
变量到底包含什么?
HttpRequestMessage
接口前缀只是
request
变量应包含的内容的配方吗?

@FunctionName("Sales")
public HttpResponseMessage run(
        @HttpTrigger(
            name = "req",
            methods = {HttpMethod.POST}, 
            authLevel = AuthorizationLevel.ANONYMOUS) 
        (1) HttpRequestMessage<Optional<String>> request,
            @RequestParameterMap
        final ExecutionContext context) 

这篇文章与我之前发表的一篇文章有些相关:azure 功能测试失败

java azure azure-functions
1个回答
0
投票

MSDOC 中所述,Azure Functions 中的

HttpRequestMessage<T>
表示一个请求,它提供了访问 HTTP 请求详细信息的方法和属性,例如:

  • HTTP 标头,允许访问
    Content-Type
    Authorization
    等信息,
  • HTTP 请求中包含的查询参数。
query = request.getQueryParameters().get("name");
  • 用于请求的 HTTP 方法,如 GET、POST、PUT、DELETE。
  • 带有路径和查询字符串的请求的 URI,可以通过
    RequestUri
    属性访问。
  • HTTP 请求的正文内容,可以使用
    getBody()
    方法访问。
name = request.getBody().orElse(query);

功能代码:

@FunctionName("HttpExample")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET, HttpMethod.POST},
                authLevel = AuthorizationLevel.ANONYMOUS)
                HttpRequestMessage<Optional<String>> request,
            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);

        if (name == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
        } else {
            return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
        }
    }

回复:

Functions:

        HttpExample: [GET,POST] http://localhost:7071/api/HttpExample

For detailed output, run func with --verbose flag.
[2025-01-22T08:58:16.984Z] Worker process started and initialized.
[2025-01-22T08:58:20.063Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2025-01-22T08:58:22.798Z] Executing 'Functions.HttpExample' (Reason='This function was programmatically called via the host APIs.', Id=20f02fae-1209-4bf5-b49e-e0c2e01e6e73)
[2025-01-22T08:58:23.117Z] Function "HttpExample" (Id: 20f02fae-1209-4bf5-b49e-e0c2e01e6e73) invoked by Java Worker
[2025-01-22T08:58:23.117Z] Java HTTP trigger processed a request.
[2025-01-22T08:58:23.331Z] Executed 'Functions.HttpExample' (Succeeded, Id=20f02fae-1209-4bf5-b49e-e0c2e01e6e73, Duration=590ms)

enter image description here

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