java - 我是否使用改造正确提交了此 POST 正文?

标签 java android retrofit

我正在尝试使用一种 API,其中端点之一接受单个 POST 正文中的多个请求,如下所示:

body:
{
  "<requestId>": {
    "actionFoo": "objId"
  },
  "<requestId>": {
    "actionBar": "objId"
  }
}

requestId 是在客户端生成的唯一字符串。我正在为这些值生成 UUID。

所以我尝试使用像这样定义的 Api 来使用 Retrofit:

首先我定义了一个“ActionCommand”对象:

public class ActionCommand {

private String actionFoo;
private String actionBar;

}

并将其输入到 Hashmap 映射中。我使用了这样定义的 api:

@POST("/path/{someObjectId}/commands")
Map<String,ActionResponse> sendActions(@Path("someObjectId")String objectId, @Body Map<String, ActionCommand> actionMap);

后端肯定正确获取了 objectId,但没有正确获取 map 中的任何操作。我不确定我还应该如何发送这些参数

谢谢

最佳答案

像这样改变你的pojo类:

package com.example;

    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    @Generated("org.jsonschema2pojo")
    public class MyPojo {

    @SerializedName("<requestId>")
    @Expose
    private RequestId requestId;

    /**
    * 
    * @return
    * The requestId
    */
    public RequestId getRequestId() {
    return requestId;
    }

    /**
    * 
    * @param requestId
    * The <requestId>
    */
    public void setRequestId(RequestId requestId) {
    this.requestId = requestId;
    }

    }

你的第二个 Pojo 将是这样的;

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class RequestId {

@SerializedName("actionBar")
@Expose
private String actionBar;

/**
* 
* @return
* The actionBar
*/
public String getActionBar() {
return actionBar;
}

/**
* 
* @param actionBar
* The actionBar
*/
public void setActionBar(String actionBar) {
this.actionBar = actionBar;
}

}

关于java - 我是否使用改造正确提交了此 POST 正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37647817/

相关文章:

java - 例如,Java 中的最佳实践是创建我自己的界面,而不是改造中现有的界面

android - 使用改造下载图像文件

java - 在 Tomcat 上使用 Jenkins 重新部署 Grails 应用程序填补了 PermGen 空间

java - Android 许可证测试响应 - 从不工作

java - 安卓ndk java.lang.UnsatisfiedLinkError : nativeStart

java - 如何在 Activity 中膨胀菜单

android - Retrofit 返回空对象,但 json 似乎有效

Java - 泛型问题

java - 如何处理多环境构建?

java - 运行应用两次即可工作