我想将我的用户发送到服务器并获取一个长号作为响应,但我在客户端遇到400错误并在服务器端获得此异常:
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of mashfood.model.User out of START_ARRAY token
我的客户端sUser模型与服务器用户模型完全相同:
sUser(或用户):
class User{
public long id;
public String username;
public String password;
public String firstName;
public String lastName;
public String gender;
public String nationalNumber;
public String phoneNumber;
public String address;
public String email;
public boolean isLoggedIn;
public String imageUrl;
public int areaNumber;
}
客户端连接到服务器,如下所示: ** tClass是类型的响应 ** params [0]是我想发送给服务器的用户 ** url + conditions是处理获取用户的方法的url
protected ResponseHandler doInBackground(Object... params) {
try {
serverResponse=new ServerResponse();
result = restTemplate.postForObject(URL + conditions, params,tClass);
serverResponse.singleResult=result;
serverResponse.isReady=true;
responseHandler.onServerRespond(serverResponse);
}catch (Exception e){
serverResponse.isReady=false;
serverResponse.message=e.getMessage();
e.printStackTrace();
}
return responseHandler;
}
和服务器端代码是:
@RequestMapping(value = "/register",method = RequestMethod.POST)
@ResponseBody
public Long registerNewUser(@RequestBody User user){
if(user!=null){
try {
System.out.println("REGISTER "+ user==null);
SessionFactory sessionFactory=HibenateConnection.connectingHibernate();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
User user1=getUser(user.username);
session.close();
return user1.id;
}catch (Exception e){
e.printStackTrace();
}
}
return -1l;
}
我的输出JSON是:
[{
"address": "",
"areaNumber": 0,
"email": "email",
"firstName": "name",
"gender": "male",
"id": -1,
"imageUrl": "img",
"isLoggedIn": false,
"lastName": "lastn",
"nationalNumber": "898898",
"password": "pass",
"phoneNumber": "936",
"username": "mehdi"
}]
我的问题在哪里?
答案 0 :(得分:0)
我的客户端JSON输出是以包含一个用户的用户列表的形式,并且我改变这样的服务器注册方法来解决我的问题:
@RequestMapping(value = "/register",method = RequestMethod.POST)
@ResponseBody
public boolean registerNewUser(@RequestBody List<User> users){
if(users!=null){
try {
SessionFactory sessionFactory=HibenateConnection.connectingHibernate();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(users.get(0));
session.getTransaction().commit();
session.close();
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
return false;
}