改造 POST 嵌套对象

Retrofit POST nested object

我想问一个关于在 Kotlin 中使用 Retrofit 2 POST 的问题

这是 Postman 所做的输入

"assets": {
       "product": [
            {
                "eTag": "\"a59d9f11f3bfc643b00a96cabb8127a6\"",
                "location": "https---abc.com/abc.png",
                "filename": "abc",
                "type": "PD"
            },
            {
                "eTag": "\"dd74783c22bdb8c7cf5ff0185297ee06\"",
                "location": "https---abc.com/abc.png",
                "filename": "abc",
                "type": "PD"
            }
       ],
       "barcode": [
           {
                "eTag": "\"3a4a0719d5fb9f5a1e4b6b9d246db4de\"",
                "location": "https---abc.com/abc.png",
                "filename": "abc",
                "type": "BC"
            }
       ]
   }

这是我的界面

    @FormUrlEncoded
    @POST("create/newassets")
    fun newAssets(
        @Field("assets") assets: Asset? = null,
    ): Call<NewSkuResponse>

这是我的 class 资产


data class Asset(

    @field:SerializedName("product")
    var product: MutableList<DataItemAsset?>,

    @field:SerializedName("barcode")
    var barcode: MutableList<DataItemAsset?>
)

我收到了 400 个错误的请求,我认为我的代码无法正常工作 任何帮助,谢谢

由 Oeganz 解决,

它与@Body 的搭配非常有效

 fun newAssets(
        @Body reqSKU: ReqSKU,
 ): Call<NewSkuResponse>
data class ReqSKU(
    @SerializedName("assets")
    val assets: Asset
)
data class Asset(

    @field:SerializedName("product")
    var product: MutableList<DataItemAsset?>,

    @field:SerializedName("barcode")
    var barcode: MutableList<DataItemAsset?>
)