【问题标题】:Android Retrofit client + Spring Boot service POST "400" responseAndroid Retrofit 客户端 + Spring Boot 服务 POST “400” 响应
【发布时间】:2018-02-27 13:28:03
【问题描述】:

我正在使用 Spring Boot API/服务 (Java) 编写 Android (Kotlin) 应用程序。 我在服务器上有适用于 Android 的 Retrofit 客户端和宁静的 Spring 控制器。

问题:当我使用下面提供的客户端-服务器 JSON 可序列化模型发出 POST 请求时,服务给出“400”(错误请求)。 目前我没有机会在服务器端记录 Spring Boot 的 web/debug 输出, 所以我唯一可以依赖和分享的就是客户端日志和实际的项目代码。

这是改造响应对象的日志,没什么特别的:

Response{protocol=http/1.1, code=400, message=, url=https://my_url}

这里是 Android 客户端的模型代码(一些具有相同名称的字段在两个模型中相互重命名以保持简单):

class ClientPrivateUserTO(val serviceId: Int?,
                             val propOne: String?,
                             val propTwo: String,
                             val propThree: String,
                             val propFour: String?,
                             val ownAIds: Iterable<Int>,
                             val ownBIds: Iterable<Int>) : Serializable

这里是改造客户的乐趣请求:

@POST("/add_update")
fun addOrUpdate(@Body userTo: ClientPrivateUserTO): Call<UserTransactionResponse>

这里是Spring服务模型类:

public class PrivateAppUserTO implements Serializable {

    private Integer serviceId;

    private String propOne;

    private String propTwo;

    private String propThree;

    private String propFour;

    private Iterable<Integer> ownAIds;

    private Iterable<Integer> ownBIds;

    public PrivateAppUserTO() {}

    public PrivateAppUserTO(@JsonProperty("serviceId") Integer serviceId, 
                            @JsonProperty("propOne") String propOne, 
                            @JsonProperty("propTwo") String propTwo, 
                            @JsonProperty("propThree") String propThree, 
                            @JsonProperty("propFour") String propFour,
                            @JsonProperty("ownAIds") Iterable<Integer> ownAIds, 
                            @JsonProperty("ownBIds") Iterable<Integer> ownBIds) {
        this.serviceId = serviceId;
        this.propOne = propOne;
        this.propTwo = propTwo;
        this.propThree = propThree;
        this.propFour = propFour;
        this.ownAIds = ownAIds;
        this.ownBIds = ownBIds;
    }

    public Integer getServiceId() {
        return serviceId;
    }

    public void setServiceId(Integer serviceId) {
        this.serviceId = serviceId;
    }

    public String getPropOne() {
        return propOne;
    }

    public void setPropOne(String propOne) {
        this.propOne = propOne;
    }

    public String getPropTwo() {
        return propTwo;
    }

    public void setPropTwo(String propTwo) {
        this.propTwo = propTwo;
    }

    public String getPropThree() {
        return propThree;
    }

    public void setPropThree(String propThree) {
        this.propThree = propThree;
    }

    public String getPropFour() {
        return propFour;
    }

    public void setPropFour(String propFour) {
        this.propFour = propFour;
    }

    public Iterable<Integer> getOwnAIds() {
        return ownAIds;
    }

    public void setOwnAIds(Iterable<Integer> ownAIds) {
        this.ownAIds = ownAIds;
    }

    public Iterable<Integer> getOwnBIds() {
        return ownBIds;
    }

    public void setOwnBIds(Iterable<Integer> ownBIds) {
        this.ownBIds = ownBIds;
    }
}

这里是服务控制器代码:

@PostMapping(path="/add_update")
public @ResponseBody TransactionalResponseUserTO createOrUpdate(@RequestBody PrivateAppUserTO userTo) {
    if (userTo.getServiceId() == null) return userService.createFromClientServerUser(userTo);
    else return userService.updateFromClientServerUser(userTo);
}

上面的代码中是否有明显错误的地方?

模型中未包装的 Iterable 可能是原因吗?

在此堆栈上编写客户端-服务器代码时我应该注意一些瓶颈吗?

非常感谢。

【问题讨论】:

    标签: android json spring spring-boot retrofit


    【解决方案1】:

    好的,似乎原因是未包装 Iterable - 现在当我切换到包装在 JSON 对象中的 List 时,所有工作都没有问题。

    【讨论】:

      猜你喜欢
      • 2020-04-16
      • 1970-01-01
      • 2021-08-04
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多