问题
The request body:
request.body {"username":"kkk.k999@gmail.com","userImage":"https://lh3.googleusercontent.com/-XdUIqdMkffA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50","role":"ROLE_USER","token":"ya29.GmIYBNhh5zs2Cpq1UI2iVzdxDvMVf2x8ggpEgM9Jvk51f5FOGodUZINrabY6K9Mhn6L82XpUhOyh5uIPhLZkAjIqS1hBu7un9QhMzRW35
RJM5ZwFozlBIuuxFRP4Y5xsTtdPGw"}
Error while Saving to Db:
@Override
public void saveUserInfo(Request request) {
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/47seconds", "root", "admin");
ObjectMapper mapper = new ObjectMapper();
try {
UserInfo userInfo = mapper.readValue(request.body(), UserInfo.class);
userInfo.set(userInfo.getUsername());
userInfo.set(userInfo.getUserImage());
userInfo.set(userInfo.getRole());
userInfo.set(userInfo.getToken());
userInfo.saveIt();
} catch (IOException ex) {
Logger.getLogger(UserServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
Base.close();
}
Error: java.lang.IllegalArgumentException: number of arguments must be even at org.javalite.activejdbc.Model.set
The number of arguments match in the model class and in the DB. The id inside database is auto incremented so I am not passing it from the code.
Model class:
@Table("user_info")
public class UserInfo extends Model {
private String username;
private String userImage;
private String token;
private String role;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the userImage
*/
public String getUserImage() {
return userImage;
}
/**
* @param userImage the userImage to set
*/
public void setUserImage(String userImage) {
this.userImage = userImage;
}
/**
* @return the token
*/
public String getToken() {
return token;
}
/**
* @param token the token to set
*/
public void setToken(String token) {
this.token = token;
}
/**
* @return the role
*/
public String getRole() {
System.out.println("printing role "+ role);
return role;
}
/**
* @param role the role to set
*/
public void setRole(String role) {
this.role = role;
}
}
回答1:
Your use of ActiveJDBC APIs is not correct.
- Do not need annotation @Table, since your model name corresponds to a table name
- Do not need to declare properties
- You call the wrong method
set
- You have spaghetti code
userInfo.set(userInfo.getUsername());
- you are setting data from an object to the same object?????
Your model needs to be (in its entirety):
public class UserInfo extends Model {}
this is all you need.
If you need to set values on a model, you can:
userInfo.set("user_name", userName);
etc.
I suggest you read the documentation and try to resolve the issues yourself, as your question displays a lack of effort on your part.
来源:https://stackoverflow.com/questions/43009622/java-lang-illegalargumentexception-number-of-arguments-must-be-even-at-org-java