英文:
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语言的模板。更多信息请参阅此文档:
原始回答:
请查看此官方模板:
我认为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:
Original Answer:
Please have a look of this offcial template:
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("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();
}
}
Please have a try, and let me know if you have some troubles.:)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论