0

I am having an issue calling a RESTful service from my client code. I have written the RESTful service using CXF/Jackson, deployed to localhost, and tested using RESTClient successfully. Below is a snippet of the service code:

@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();
}

The model class and dao work successfully and the service itself works fine using RESTClient. However, when I attempt to call this service from Java Script, I get the error below on the server side:

Caused by: org.codehaus.jackson.JsonParseException: Unexpected character ('m' (code 109)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')

I have copied the client side code below. To make sure it has nothing to do with the JSON data itself, I used a valid JSON string (which works using RESTClient, JSON.parse() method, and JSONLint) in the vars 'json' (string) and 'jsonData' (JSON). Below is the Java Script code:

//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"
    });

I've seen the JsonParseException a number of times on other threads, but in this case the JSON itself appears to be valid (and tested). Any thoughts are appreciated.

Here is the model class.

public class 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;
}

}

4

2 回答 2

1

您需要检查确切的内容服务器正在获取什么:这不是您的代码示例显示的内容。

于 2013-10-23T05:13:19.567 回答
0

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

var jsonToSend = new Object();
jsonToSend.mood_value = 8;
jsonToSend.mood_comments = "new comments";
.... 
于 2013-10-23T10:18:36.050 回答