将json从Unity发送到SpringBoot Rest -

时间:2018-03-17 01:27:31

标签: java spring-boot unity3d

我正试着打电话给我为我的团结游戏做的休息终点,但是任何时候我通过一个Rest函数将用户的json细节通过Unity发送到我的Springboot应用程序我得到了错误......

  

2018-03-17 02:20:40 [thread] WARN oswsmmaExceptionHandlerExceptionResolver - 由Handler执行导致的已解决异常:org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON文档:意外字符('%'(代码37)):期望一个有效值(数字,字符串,数组,对象,'真','假'或'空')    在[来源:java.io.PushbackInputStream@69ce261; line:1,column:2];嵌套异常是com.fasterxml.jackson.core.JsonParseException:意外字符('%'(代码37)):预期有效值(数字,字符串,数组,对象,'true','false'或'null')    在[来源:java.io.PushbackInputStream@69ce261; line:1,column:2]

这是我的Unity代码

public IEnumerator CallLogin(string Username, string Password) {


    UnityWebRequest www = 
  UnityWebRequest.Post("http://localhost:8080/unity/login", new 
     User(Username, Password).ToJson()); 

    www.uploadHandler.contentType = "application/json";

    yield return www.Send();

    if (www.error != null)
    {

        Debug.Log("Error " + www.error);

    }
    else {

        Debug.Log("Response  " + www.downloadHandler.text);

    }

}

这是我的Springboot java代码

@ResponseBody
@RequestMapping(value = "/login", method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UserModel> Login(@RequestBody User user){

    // check if the username and password provided matches a user in the database.
    String verifiedResponse = this.defaultUserDAOService.verification(user);

    UserModel userModel = new UserModel();

    if(verifiedResponse.equalsIgnoreCase("Verified")){ // if the user was verified successfully (Found in the database)

        userModel.setUser(defaultUserDAOService.getByUsername(user.getUserName()))
                .setSuccessful(true)
                .setResponseMessage("Successful");


        return new ResponseEntity<>(userModel, HttpStatus.OK); // return response to client.


    }else{

        userModel.setUser(new User())
                .setSuccessful(false)
                .setResponseMessage("Username or Password is incorrect");

        return new ResponseEntity<>(userModel, HttpStatus.NOT_FOUND); // return response to client.

    }


}

最后是具有ToJson功能的User类

[序列化] 公共课用户:MonoBehaviour {

public User(string userName, string password)
{
    this.userName = userName;

    this.password = password;
}

public string userName;

public string password;


public string ToJson()
{
    return JsonUtility.ToJson(this);
}

}

我需要帮助,我已经和我斗争了好几个小时。

0 个答案:

没有答案