Json将对象发送到Java EE服务器

时间:2016-03-07 18:38:04

标签: java ios json jax-rs

我正在尝试通过Json将一个简单的对象从IOS客户端发送到JAX-RS资源。客户端的json看起来像这样:

{"longitude": -122.136034834497, "latitude": 37.3666157838699}

JAX-RS方法看起来像这样:

@Path("/save")
@POST
@Produces("application/json")
@Consumes("application/json")
public MyObject saveQuizWalk(MyObject obj)
        throws JSONException {

    return myBean.saveObject(obj);
}

java类看起来像这样:

public class MyObject {

private double longitude, latitude;

-----get/set------

如果我将服务器端的对象转换为json,则json-string的格式如下:

{"longitude":11.11,"latitude":12.11}

来自客户端的请求如下所示:

        // Convert object to json
    let json = JSONSerializer.toJson(myObject.createJsonFriendlyObject()                   )
    print(json)
    // create the request & response
    let request = NSMutableURLRequest(URL: NSURL(string: "someurl")!)
    var response: NSURLResponse?


    // create some JSON data and configure the request

    request.HTTPBody = json.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
    request.HTTPMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    do {
        // send the request
        try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)
    }
    catch {

    }

服务器的响应是400.为什么会这样?

汉克

0 个答案:

没有答案