0

我正在使用带有 Almofire 5.0.2 的 Swift 5 开发 iOS 项目。我尝试了与此类似的在线线程上建议的所有选项,但看起来我仍然缺少一些东西。

我在 POST 方法 API 调用期间遇到困难,其中包含一组项目作为请求正文的一部分。

API 看起来像:

@RequestMapping(...)
SomeResponse doSomething (@RequestBody SomeRequest request) {}

我正在使用 Swift 5 和 alamo fire 5.0.2 版本,我尝试了上面的选项和下面的选项,但它根本没有命中 API(返回 AFError):

let parameters: [String: Any] = [
            "block": true,
            "items": [],
            "name": "xxxxx",
            "mobile": "xxxxxxxxxx",
            "email": "xxxx@gmail.com",
            "List": [["place": "london"],["place": "holand"]]

  ]

AF.request(BookingConstants.httpEndpoint, method: .post, parameters: parameters, headers: headers)
            .validate()
            .responseData { response in

}

我在 java spring boot API 端收到此错误:

Invalid index in property path 'item[][place]'; nested exception is java.lang.NumberFormatException: For input string: ""] with root cause

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:1.8.0_221]
    at java.lang.Integer.parseInt(Integer.java:592) ~[?:1.8.0_221]
    at java.lang.Integer.parseInt(Integer.java:615) ~[?:1.8.0_221]
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:663) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyHoldingValue(AbstractNestablePropertyAccessor.java:402) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.processKeyedProperty(AbstractNestablePropertyAccessor.java:298) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:289) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:859) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:755) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:192) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:106) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE

我知道我错过了一些东西,但无法弄清楚。有人可以帮我解决这个问题吗?如果有任何意见/建议,请告诉我。提前致谢

4

1 回答 1

0

我切换回 Alamofire 4.0.1 版本并使用 Alamofire 上传方法来实现这一点。

let objectAsJSON: JSON = request.toJSON()
let data: Data = try objectAsJSON.serialize()

Alamofire.upload(data, to: BookingConstants.httpEndpoint, headers: headers)
                .validate()
                .responseData { response in
        // response code logic

}
于 2020-04-12T19:54:06.320 回答