azure - 在使用 JAVA 的 azure 函数中,如何获取请求正文
问题描述
在使用 JAVA 的 azure 函数中,如何获取以 json 形式发送的请求有效负载
我的代码是这样的:
@FunctionName("hello")
public HttpResponseMessage<String> hello(
@HttpTrigger(name = "req", methods = {"post"}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,final ExecutionContext context) throws Exception {
context.getLogger().info("Java HTTP trigger processed a request.");
System.out.println("**********REQUEST BODY*************"+request.getBody());
String cookie = new Authenticate().authenticateAndGetCookie();
ExecuteCommands commandsExecutor = new ExecuteCommands(cookie);
String s=commandsExecutor.control(request.toString());
System.out.println(s);
JSONObject jobj=new JSONObject(s);
if (s == null) {
return request.createResponse(400, "Please pass a name on the query string or in the request body");
} else {
return request.createResponse(200,jobj.toString());
}
}
由于使用了 HttpRequestMessage>,当我在控制台上打印正文时,就像我在上面粘贴的代码中一样,我在控制台中获取它。
**********REQUEST BODY*************Optional[{
[13-06-2018 11:40:18] Java HTTP trigger processed a request.
[13-06-2018 11:40:18] "Command": "com",
[13-06-2018 11:40:18] "Id": "id",
[13-06-2018 11:40:18] "Id2": "id2",
[13-06-2018 11:40:18] "Operation": "op"
[13-06-2018 11:40:18] }]
但我通过了以下有效载荷:
{
"Command": "com",
"Id": "id",
"Id2": "id2",
"Operation": "op"
}
所以,我尝试使用 HttpRequestMessage 但我得到了以下异常
incompatible types: com.microsoft.azure.serverless.functions.HttpRequestMessage<java.util.Optional<java.lang.String>> cannot be converted to com.microsoft.azure.serverless.functions.HttpRequestMessage<java.lang.String>
解决方案
推荐阅读
- c# - 使用 Sqlite 命令 C# foreach 进入 foreach
- python-3.x - 有没有办法在 IIS 服务器上部署 FastAPI?
- css - 如何通过点击 :before 内容导航到另一个页面?
- python - 使用 in_training_phase 的 Keras 自定义损失函数包装器
- arrays - Angular - 根据其索引与另一个数组中的值匹配的对象数组中的元素删除
- ios - 已删除的模块或框架仍显示在导入语句中
- java - 在 Spring Boot 应用程序上设置 Logstash
- r - dplyr 基于差异丢弃值
- .net - 我能否提供一个没有 DLL 的 .NET 服务,这些 DLL 需要编译但在运行时不需要?
- mysql - 我正面临使用 hibernate5 和 MySql 的 Spring Boot 更新查询错误