1、问题:如何让Jackson 对枚举中中特定字段进行解析?
经常我们用枚举来表示某组相关属性值。
举个栗子:
/**
* 性别类型
*/
public enum GenderType {
GENDER_UNKNOWN(0, "未知"),
GENDER_MALE(1, "男性"),
GENDER_FEMAL(2, "女性"),
GENDER_OTHER(3, "其他"),
;
private int value;
private String comment;
private GenderType(int value, String comment){
this.value = value;
this.comment = comment;
}
@JsonValue
public int getValue() {
return value;
}
public String getComment() {
return comment;
}
}
这里,我们希望Jackson解析GenderType时,只将对应类型的value解析出来,通过在属性的getValue()方法上加@JsonValue,这样设置的枚举值在通过Jackson解析时,会自动将枚举替换成对应的value值。
举个栗子:public class Student {
private String name;
private GenderType genderType;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public GenderType getGenderType() {
return genderType;
}
public void setGenderType(GenderType genderType) {
this.genderType = genderType;
}
public static void main(String[] args) throws Exception{
Student student = new Student();
student.setName("tim");
student.setGenderType(GenderType.GENDER_FEMAL);
System.out.println(JsonUtils.writeObject2Json(student));
}
}
结果输出:
{"name":"tim","genderType":2}
分析:上面我们将Student的类的性别字段设置为枚举类型,在通过Jackson解析成json数据时,因为设置了@JsonValue因此会解析成我们需要的值。
2、问题:如何让Jackson 当转换对象为json时,如果属性为null时,不输出?
通过在实体前加入:@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
举个栗子:
Student.java 类:@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Student {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
;
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
public static void main(String[] args) throws JsonProcessingException {
Student student = new Student();
student.setName("tim");
System.out.println(JsonUtils.writeObject2Json(student));
}
}
输出:
{"name":"tim"}
分析:
上述通过加入@JsonSerialize(include =JsonSerialize.Inclusion.NON_NULL)注解,这样当我们的示例student虽然只设置name字段值为”tim”,此时,sex字段的值为null,但是Jackson通过上述注解忽略了字段值为null。
3、问题:更改字段的显示值?
通过加入注解:@JsonProperty(“”)
举个栗子:@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Student {
@JsonProperty("error_code")
private int errorCode;
private String name;
private String sex;
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
;
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
public static void main(String[] args) throws JsonProcessingException {
Student student = new Student();
student.setErrorCode(0);
student.setName("tim");
System.out.println(JsonUtils.writeObject2Json(student));
}
}
输出:
{"name":"tim","error_code":0}
分析:
通过加入@JsonProperty("error_code")注解,能将之前字段名为errorCode,显示的时候改为error_code。