当我使用spring mvc时。我想得到json string.But遇到了问题。
我有一个班级用户:
public class User {
private String username;
private String password;
private boolean accountNonExpired = true;
private boolean accountNonLocked = true;
private boolean credentialsNonExpired = true;
private boolean enabled = true;
private DateTime registeTime;
private String ip;
private DateTime loginTime;
private DateTime logoutTime;
private String loginIp;
private DateTime lastLogoutTime;
private int passwordExpiredDays = -1;
private DateTime passwordChangeTime;
private boolean loginAtSameTime = false;
private int loginAttempt = 0;
private int status = 0;
private String problem;
private String solution;
}
我想要像json一样:
{"username":"a","ip":"127.0.0.1"}
现在,我使用:
@JsonIgnoreProperties(value = {"password","accountNonExpired",
"accountNonLocked","credentialsNonExpired", "enabled", "registeTime",
"loginTime", "logoutTime", "passwordExpiredDays", "passwordChangeTime",
"loginAtSameTime", "loginAttempt", "status", "problem", "solution"})
public class User {
...
}
但是!!!这非常复杂。我能找到解决方法吗?例如:
@JsonInclude("Annotation")
public class User {
@JsonInclude
private String username;
...
@JsonInclude
private String ip;
...
}
或
@JsonIncludeProperties(value={"username", "ip"})
public class User {
...
}
答案 0 :(得分:0)
我认为你可以在字段级别使用@JsonIgnore
注释来避免在序列化过程中使用它。
答案 1 :(得分:0)
我相信你需要做的就是用@JsonProperty
标记你想要的字段。例如:
public class User {
@JsonProperty
private String username;
...
@JsonProperty
private String ip;
...
}