无法从 VS Code 中的 Java 本地运行访问 Azure 函数。

huangapple go评论97阅读模式
英文:

Not able to hit azure function in local running from vs code in java

问题

public class Function {
    @FunctionName("hello")
    public HttpResponseMessage hello(@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage req,
                       final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        String query = req.getQueryParameters().get("name").toString();

        return req.createResponse(200, "hELLO " + query);
    }
}
英文:

I am facing issue while calling an azure function developed in java with VS Code. Everything goes fine except the run command. When I run the command 'mvn azure-functions:run', it starts properly. But I am not able to hit it from postman. Here is my code.

public class Function {
    @FunctionName("hello")
    public HttpResponseMessage hello(@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage req,
                       final ExecutionContext context) {
        context.getLogger().info("Java HTTP triger processed a request.");

        String query = req.getQueryParameters().get("name").toString();

        return req.createResponse(200, "hELLO "+query);
    }
}

答案1

得分: 0

Update:

感谢Satya Panigrahy的分享,解决方案是运行mvn generate来生成Azure函数。顺便提一下,Azure函数工具没有Java语言的模板。更多信息请参阅此文档:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-kotlin-maven?tabs=cmd#generate-a-new-functions-project

原始回答:

请查看此官方模板:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=java#example

我认为HttpRequestMessage没有名为createResponse的方法。如果您想实现,请尝试下面的代码,在我的一侧效果很好:

package com.function;

import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

public class Function {
    @FunctionName("hello")
    public HttpResponseMessage run(
            @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> req,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        String query = req.getQueryParameters().get("name").toString();
        return req.createResponseBuilder(HttpStatus.OK).body("hELLO " + query).build();
    }
}

请尝试一下,如果遇到问题,请告诉我。:)

英文:

Update:

Thank's for Satya Panigrahy's sharing, the solution is to run mvn generate to generate azure function. By the way, azure function tools dont have a template of java language. For more information, please have a look of this doc:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-kotlin-maven?tabs=cmd#generate-a-new-functions-project

Original Answer:

Please have a look of this offcial template:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=java#example

I think the HttpRequestMessage dont have a method named createResponse. If you want to achieve, please have a try of the below codes, it works fine on my side:

package com.function;

import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;


public class Function {
    @FunctionName(&quot;hello&quot;)
    public HttpResponseMessage run(
            @HttpTrigger(name = &quot;req&quot;, methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage&lt;Optional&lt;String&gt;&gt; req,
            final ExecutionContext context) {
        context.getLogger().info(&quot;Java HTTP trigger processed a request.&quot;);

        String query = req.getQueryParameters().get(&quot;name&quot;).toString();
        return req.createResponseBuilder(HttpStatus.OK).body(&quot;hELLO &quot; + query).build();
    }
}

无法从 VS Code 中的 Java 本地运行访问 Azure 函数。

无法从 VS Code 中的 Java 本地运行访问 Azure 函数。

Please have a try, and let me know if you have some troubles.:)

huangapple
  • 本文由 发表于 2020年5月3日 23:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61576808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定