I am trying to a call a rest end point I made for my unity game, but any time I send the users json details via unity to my Springboot application through a Rest call i get the error ...
2018-03-17 02:20:40 [thread] WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver -Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: java.io.PushbackInputStream@69ce261; line: 1, column: 2]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: java.io.PushbackInputStream@69ce261; line: 1, column: 2]
This it my Unity code
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);
}
}
And this is my Springboot java code
@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.
}
}
Finally the User class which has the ToJson function
[Serializable] public class 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);
}
}
I need help please, i have been battling this for hours.