java - 如何使用 POST 将 Array 发送到 REST-Service (Jersey)

标签 java arrays rest post jersey

我想使用 JQuery 将位置数组发送到 REST 服务。

我的代码

var locations = new Array();
var loc = new Location();
loc.alt = 0;
loc.lat = 0;
loc.long = 0;
loc.time = 1;

locations.push(loc);

var json_data = {value : JSON.stringify(locations)};
console.log(json_data);

$.ajax({type: 'POST',
        url: rootURL + '/Location/123',
        crossDomain: true,
        dataType: "json", // data type of response
        contentType: "application/json; charset=utf-8",
        data: json_data,
        success: function(data, textStatus, jqXHR) {
            console.log('testAddLocations erfolgreich');
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log('testAddLocations Fehler');
        }
    });

REST-服务
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Path("{tripID}")
public Response addLocations(@PathParam("tripID") final String tripID, Location[] value) {

但是我收到一个 HTTP-500 错误。在服务器上:
SCHWERWIEGEND: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
JsonFormatException{text=v, line=1, column=1}
at com.sun.jersey.json.impl.reader.JsonLexer.yylex(JsonLexer.java:662)
at com.sun.jersey.json.impl.reader.JsonXmlStreamReader.nextToken(JsonXmlStreamReader.java:162)

最佳答案

我假设这是因为您发送数据的格式。您发布的数据不应该是整个字符串化的 JSON 吗? (包括 value 通常应该是 location ?)

我的意思是 Jersey 使用不同的 JSON notations序列化/反序列化消息,因此,例如,如果您使用 MAPPED 表示法,则应发送如下内容:

{"location":[
    {"alt":1,"lat":2,"long":3,"time":4},
    {"alt":5,"lat":6,"long":7,"time":8}
]}

这意味着使用 jQuery 编写这样的代码:
$.ajax({
    data : JSON.stringify( 
    {"location" : [
        { alt: 1, lat: 2, long: 3, time: 4 },
        { alt: 5, lat: 6, long: 7, time: 8 }
    ]}),
    ....

对于自然符号,您应该发送:
[
    {"alt":1,"lat":2,"long":3,"time":4},
    {"alt":5,"lat":6,"long":7,"time":8}
]

这意味着代码如下:
$.ajax({
    data : JSON.stringify( 
    [
        { alt: 1, lat: 2, long: 3, time: 4 },
        { alt: 5, lat: 6, long: 7, time: 8 }
    ]),
    ...

查看默认行为的一种快速方法是将其添加到您的服务中并查看它返回的格式:
@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("/testNotation")
public Location[] getLocations() {
    return new Location[] { new Location(), new Location() };
}

请参阅此处了解一些额外信息:

http://jersey.java.net/nonav/documentation/latest/json.html
http://jersey.java.net/nonav/apidocs/1.17/jersey/com/sun/jersey/api/json/JSONConfiguration.html
http://jersey.java.net/nonav/apidocs/1.17/jersey/com/sun/jersey/api/json/JSONConfiguration.Notation.html
http://tugdualgrall.blogspot.ro/2011/09/jax-rs-jersey-and-single-element-arrays.html

关于java - 如何使用 POST 将 Array 发送到 REST-Service (Jersey),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14893932/

相关文章:

java - 如何修复此Java代码以播放音频文件?

java - 从特定模式拆分字符串

javascript - 对象数组 - 与其他列数据相比增加一列

Heroku 上的 Java TCP/UDP 服务器和客户端

java - 通过 JSON Rest Server (Jersey) 和 Android 进行通信

java - 使用 Flex 创建 REST 客户端是否可行?

java - 的意思 ?在 Map<String, ?>

java - Google Books Api - imageLinks 没有值(value) - Android JSONException

php - 在MySQL多重插入中将数组值连接到字符串中

JAVA 所以我有这段代码,我正在使用 [60] 数组,当我使用 add 命令时,它会重写我之前所做的所有语句