0

我在下面做错了什么?

客户端代码

  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me?fields=id,name,first_name,last_name,username',
          function(response) {
    var User  = {
    accessToken: FB.getAuthResponse()['accessToken'],
    fbUserId: response.id,
    userName: response.username,
    firstName:response.first_name,
    lastName:response.last_name
    }

    console.log(User);

    $.ajax({ 
        type: "POST",
        url: "http://localhost:8080/myapp/api/1.0/user/",
        data: User,
        dataType : 'json',
        success: function(apiResponse)
                    {                           
            var session = apiResponse.sessionId;
            console.log ("Session : "+ session);
        }
    });
});

}

服务器端

   public ResponseEntity<String> createOrUpdateUser(@RequestBody String json) 
   {
    ResponseEntity<String> responseEntity = null;

   User user = gson.fromJson(json, User.class); 
    .....
   }

在 gson.fromJson 上,我收到以下错误

SEVERE: Servlet.service() for servlet [analytics] in context with path [/myapp] threw exception [Request processing failed; nested exception is com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 9] with root cause
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 9
at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
at com.google.gson.Gson.fromJson(Gson.java:795)
at com.google.gson.Gson.fromJson(Gson.java:761)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
at com.myapp.controller.UserController.createOrUpdateUser(UserController.java:104)

===========================================

编辑 - 一个可能的解决方案

以下工作:

var User  = {
    accessToken: FB.getAuthResponse()['accessToken'],
    fbUserId: response.id,
    userName: response.username,
    firstName:response.first_name,
    lastName:response.last_name
};

$.ajax({
     type: "POST",
     url: "http://localhost:8080/myapp/api/1.0/user/",
     data: JSON.stringify(User),
     dataType : "json",
      success: function(apiResponse) {
            var session = apiResponse.sessionId;
            console.log ("Session : "+ session);
        },
        error: function(apiResponse) {
            //alert("error");
            alert("error  : " +apiResponse);
        }
    });

在 Java 端

public ResponseEntity<String> createOrUpdateUser(
        @RequestBody String json
        ) {

    ResponseEntity<String> responseEntity = null;

    String result = "";
    try{
        result = URLDecoder.decode(json, "UTF-8");
    } catch (UnsupportedEncodingException e)
    {

    }
   String [] parts = result.split("=");
   User user = gson.fromJson(parts[0], User.class); 

我仍在试图弄清楚我如何最终在 URL 的末尾得到“=”。

4

1 回答 1

0

尝试数据:{json:JSON.stringify(User)}

于 2013-07-26T15:13:32.443 回答