Java 反序列化时的改装/Jackson错误
我试图从我的web服务获取JSON并反序列化到我的类UserSync,但我得到以下错误: 无法从字符串值(“”)实例化类型[simple type,class com.example.breno.teste.model.User]的值;没有单字符串构造函数/工厂方法 在[来源:okhttp3.responseBook]$BomAwareReader@fbf814f;行:1,列:86](通过引用链:com.example.breno.teste.dto.UserSync[“user”]) 我读过一些帖子说我需要在UserSync中声明我的用户类static,但当我这样做时,jackson找不到任何用户属性,即使使用JsonDescription。另一篇文章说我可能需要声明一个默认构造函数,所以我这样做了 以下是UserSync类:Java 反序列化时的改装/Jackson错误,java,android,json,jackson,retrofit,Java,Android,Json,Jackson,Retrofit,我试图从我的web服务获取JSON并反序列化到我的类UserSync,但我得到以下错误: 无法从字符串值(“”)实例化类型[simple type,class com.example.breno.teste.model.User]的值;没有单字符串构造函数/工厂方法 在[来源:okhttp3.responseBook]$BomAwareReader@fbf814f;行:1,列:86](通过引用链:com.example.breno.teste.dto.UserSync[“user”]) 我读过一
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserSync {
@JsonProperty("status")
private String Status;
@JsonProperty("currentDate")
private String CurrentDate;
@JsonProperty("message")
private String Message;
@JsonProperty("user")
private static User NewUser;
public UserSync() {
}
public String getStatus() {
return Status;
}
public String getCurrentDate() {
return CurrentDate;
}
public String getMessage() {
return Message;
}
public static User getNewUser() {
return NewUser;
}
用户类:
public class User implements Serializable {
@JsonProperty("userKey")
private UUID UserKey;
@JsonProperty("userPassword")
private String UserPassword;
@JsonProperty("userGroupKey")
private UUID UserGroupKey;
@JsonProperty("signInDate")
private String SignInDate;
@JsonProperty("active")
private boolean Active;
@JsonProperty("profilePicturePath")
private String ProfilePic;
@JsonProperty("completeName")
private String UserCompleteName;
@JsonProperty("email")
private String UserEmail;
@JsonProperty("isLogged")
private boolean IsLogged;
public User() {
}
public boolean getIsLogged() {
return IsLogged;
}
public void setIsLogged(boolean isLogged) {
IsLogged = isLogged;
}
public String getUserEmail() {
return UserEmail;
}
public void setUserEmail(String userEmail) {
UserEmail = userEmail;
}
public UUID getUserKey() {
return UserKey;
}
public void setUserKey(UUID userKey) {
UserKey = userKey;
}
public String getUserPassword() {
return UserPassword;
}
public void setUserPassword(String userPassword) {
UserPassword = userPassword;
}
public UUID getUserGroupKey() {
return UserGroupKey;
}
public void setUserGroupKey(UUID userGroupKey) {
UserGroupKey = userGroupKey;
}
public String getSignInDate() {
return SignInDate;
}
public void setSignInDate(String signInDate) {
SignInDate = signInDate;
}
public boolean getActive() {
return Active;
}
public void setActive(boolean active) {
Active = active;
}
public String getProfilePic() {
return ProfilePic;
}
public void setProfilePic(String profilePic) {
ProfilePic = profilePic;
}
public String getUserCompleteName() {
return UserCompleteName;
}
public void setUserCompleteName(String userCompleteName) {
UserCompleteName = userCompleteName;
}
}
我的服务类(现在使用postNewUser):
有人能帮我吗
编辑1-以下是我用来进行改装调用的方法:
public void register(User user) {
Call<UserSync> postCall = new RetrofitInitializator().getUserService().postNewUser(user);
postCall.enqueue(getRegisterCallback());
}
@NonNull
private Callback<UserSync> getRegisterCallback() {
return new Callback<UserSync>() {
@Override
public void onResponse(Call<UserSync> call, Response<UserSync> response) {
User user = response.body().getNewUser();
}
@Override
public void onFailure(Call<UserSync> call, Throwable t) {
Log.e("Register - onFailure", t.getMessage());
}
};
}
我设法解决了将类型User切换到JsonNode并在此之后进行转换的问题
@JsonProperty("status")
private String Status;
@JsonProperty("currentDate")
private String CurrentDate;
@JsonProperty("message")
private String Message;
@JsonProperty("user")
private JsonNode NewUser;
以及转换:
private User getUserFromUserAsync(UserSync userSync) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.treeToValue(userSync.getNewUser(), User.class);
}
我设法解决了将类型User切换到JsonNode并在此之后进行转换的问题
@JsonProperty("status")
private String Status;
@JsonProperty("currentDate")
private String CurrentDate;
@JsonProperty("message")
private String Message;
@JsonProperty("user")
private JsonNode NewUser;
以及转换:
private User getUserFromUserAsync(UserSync userSync) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.treeToValue(userSync.getNewUser(), User.class);
}
演示如何创建改装服务类(UserService)@MatiasElorriaga在调用中添加了编辑1。请添加改装初始化器代码。。我想看看你是否在添加jacksonconverter@MatiasElorriaga,有。我认为您需要在User中添加一个构造函数,该构造函数将字符串作为参数,并将该字符串解析为用户对象。演示如何创建改装服务类(UserService)@MatiasElorriaga在调用中添加了编辑1。请添加初始化器代码。。我想看看你是否在添加jacksonconverter@MatiasElorriaga我认为您需要在User中添加一个构造函数,该构造函数将字符串作为参数,并将该字符串解析为User对象。
private User getUserFromUserAsync(UserSync userSync) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.treeToValue(userSync.getNewUser(), User.class);
}