为什么我的响应数据为零?

问题描述 投票:0回答:1

[通过Spring Boot应用程序使用iOS Alamofire。

客户端应用


型号

class User: Codable {
    var name: String
    var email: String
    var token: String
    var password: String
    var loginStatus: Int
}

class UserResponseData: Codable {
    let body: User?
}

通话请求

private func makeRequest(_ path: String, method: HTTPMethod, params: Parameters?, headers: HTTPHeaders?, completion: @escaping(User?) -> Void) {
    let enc = JSONEncoding.default
    let url = AppConstants.ENDPOINT + path //path = signup

    Alamofire.request(url, method: .post, parameters: params, encoding: enc, headers: headers)
     .validate()
     .responseJSON { response in
         switch (response.result) {
         case .success( _):
                do {
                    // issue userData becomes nil but no parsing error
                    let userData = try JSONDecoder().decode(UserResponseData.self, from: response.data!)

                } catch let error as NSError {
                    print("Failed to load: \(error.debugDescription)")
                }
             case .failure(let error):
                print("Request error: \(error.localizedDescription)")
         }
     }
}

服务器应用


型号

public class RestResponse {

    private String status = "failed";
    private String errorMessage = "Error";
    private ResponseEntity<?> data = null;

    public RestResponse() {}

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public ResponseEntity<?> getData() {
        return data;
    }

    public void setData(ResponseEntity<?> data) {
        this.data = data;
    }
}

控制器

@RestController
@RequestMapping("/signup")
public class SignupController {

    @Autowired
    public UserDao userDao;

    @PostMapping()
    ResponseEntity<RestResponse> signup(@RequestBody User newUser) {
        User user = userDao.addUser(newUser);
        user.setPassword("******");

        RestResponse response = new RestResponse();
        response.setStatus("success");
        response.setErrorMessage(null);
        response.setData(ResponseEntity.ok().body(user));
        return ResponseEntity.ok().body(response);
    }
}

我在Alamofire响应中获得userData的零值,而没有任何解析错误。

let userData = try JSONDecoder().decode(UserResponseData.self, from: response.data!)

尽管我得到了邮递员的有效回复,

{
    "status": "success",
    "errorMessage": null,
    "data": {
        "headers": {},
        "body": {
            "name": "Elon",
            "email": "[email protected]",
            "password": "******",
            "token": "Elon Token",
            "loginStatus": 0
        },
        "statusCode": "OK",
        "statusCodeValue": 200
    }
}

我想念什么?

ios json spring-boot parsing alamofire
1个回答
0
投票

最后我已经通过添加根对象解决了该问题,

class User: Codable {
    var name: String
    var email: String
    var token: String
    var password: String
    var loginStatus: Int
}

class UserResponseData: Codable {
    let body: User?
}

class UserResponseRoot: Codable {
    let data: UserResponseData
}

通话,

let userData = try JSONDecoder().decode(UserResponseRoot.self, from: response.data!)
© www.soinside.com 2019 - 2024. All rights reserved.