有效JSON上的JsonParseException

时间:2013-10-23 03:49:27

标签: ajax json rest jackson cxf

我遇到从客户端代码调用RESTful服务的问题。我使用CXF / Jackson编写了RESTful服务,部署到localhost,并成功使用RESTClient进行了测试。以下是服务代码的片段:

@POST
@Produces("application/json")
@Consumes("application/json")
@Path("/set/mood")
public Response setMood(MoodMeter mm) {

    this.getMmDAO().insert(mm);
    return Response.ok().entity(mm).build();
}

模型类和dao成功运行,服务本身使用RESTClient正常工作。但是,当我尝试从Java Script调用此服务时,我在服务器端收到以下错误:

引起:org.codehaus.jackson.JsonParseException:意外字符('m'(代码109)):预期有效值(数字,字符串,数组,对象,'true','false'或'null' )

我已经复制了下面的客户端代码。为了确保它与JSON数据本身无关,我在vars'json'(字符串)和'jsonData'中使用了一个有效的JSON字符串(使用RESTClient,JSON.parse()方法和JSONLint工作)( JSON)。以下是Java Script代码:

//var json = '{"mood_value":8,"mood_comments":"new comments","user_id":5,"point":{"latitude":37.292929,"longitude":38.0323323},"created_dtm":1381546869260}';
//var jsonData = JSON.parse(json);

var pointData = new Object();
pointData.latitue = 23.43433;
pointData.longitude = 25.393939;

var jsonData = new Object();
jsonData.mood_value = 8;
jsonData.mood_comments= "sample comment";
jsonData.user_id = 10;
jsonData.point = pointData;
jsonData.created_dtm = 1381546869260;

$.ajax({
      url: 'http://localhost:8080/moodmeter/app/service/set/mood',
      dataType: 'json',
      data: jsonData,
      type: "POST",
      contentType: "application/json"
    });

我在其他线程上多次看到JsonParseException,但在这种情况下,JSON本身似乎是有效的(并经过测试)。任何想法都表示赞赏。

这是模型类。

公共课MoodMeter {

Point point;
int user_id;
int mood_value;
Timestamp created_dtm;
String mood_comments;

public MoodMeter() {
    super();
}

public MoodMeter(Point point, int user_id, int mood_value,
        Timestamp created_dtm, String mood_comments) {
    super();
    this.point = point;
    this.user_id = user_id;
    this.mood_value = mood_value;
    this.created_dtm = created_dtm;
    this.mood_comments = mood_comments;
}

public Point getPoint() {
    return point;
}
public void setPoint(Point point) {
    this.point = point;
}
public int getUser_id() {
    return user_id;
}
public void setUser_id(int user_id) {
    this.user_id = user_id;
}
public int getMood_value() {
    return mood_value;
}
public void setMood_value(int mood_value) {
    this.mood_value = mood_value;
}
public Date getCreated_dtm() {
    return created_dtm;
}
public void setCreated_dtm(Timestamp created_dtm) {
    this.created_dtm = created_dtm;
}
public String getMood_comments() {
    return mood_comments;
}
public void setMood_comments(String mood_comments) {
    this.mood_comments = mood_comments;
}

}

2 个答案:

答案 0 :(得分:1)

您需要检查服务器获取的确切内容:它不是您的代码示例所显示的内容。

答案 1 :(得分:0)

尝试通过创建如下对象来发送有效的jsonData:

var jsonToSend = new Object();
jsonToSend.mood_value = 8;
jsonToSend.mood_comments = "new comments";
....