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); 
    .....
   }

on gson.fromJson, I get following error

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)

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

Edit - A possible solution

Following worked :

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

On Java Side

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); 

I am still trying to figure how do i end up getting "=" at the end of URL.

4

1 に答える 1

0

データを試してください: {json : JSON.stringify(User)}

于 2013-07-26T15:13:32.443 に答える