参考文章:https://blog.csdn.net/weixin_49969179/article/details/126369966?spm=1001.2014.3001.5502
javapackage com.fasterxml.jackson.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonAlias {
String[] value() default {};
}
javaimport com.fasterxml.jackson.annotation.JsonAlias;
import com.integration.api.utils.JackJsonUtils;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class TestUser {
@JsonAlias("xm")
private String name;
public static void main(String[] args) {
String json = "{\"xm\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"name\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
}
}
{"name":"张三"} {"name":"张三"}
javaimport com.fasterxml.jackson.annotation.JsonAlias;
import com.integration.api.utils.JackJsonUtils;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class TestUser {
@JsonAlias({"xm", "mc", "username", "NAME"})
private String name;
public static void main(String[] args) {
String json = "{\"name\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"xm\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"mc\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"username\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"NAME\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
}
}
{"name":"张三"} {"name":"张三"} {"name":"张三"} {"name":"张三"} {"name":"张三"}
比如你原来小名叫【李狗蛋】,使用JsonAlias给你赋予别名【李世民】后,Jackson反序列化时知道【李狗蛋】【李世民】都是你。
javapackage com.fasterxml.jackson.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonProperty {
String USE_DEFAULT_NAME = "";
int INDEX_UNKNOWN = -1;
String value() default "";
boolean required() default false;
int index() default -1;
String defaultValue() default "";
JsonProperty.Access access() default JsonProperty.Access.AUTO;
public static enum Access {
AUTO,
READ_ONLY,
WRITE_ONLY,
READ_WRITE;
private Access() {
}
}
}
javaimport com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.integration.api.utils.JackJsonUtils;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class TestUser {
@JsonAlias({"xm", "mc", "username", "NAME"})
@JsonProperty("xingming")
private String name;
public static void main(String[] args) {
String json = "{\"name\":\"张三\"}";
// System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"xm\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"mc\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"username\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"NAME\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
json = "{\"xingming\":\"张三\"}";
System.out.println(JackJsonUtils.toJSONString(JackJsonUtils.parseObject(json, TestUser.class)));
}
}
{"xingming":"张三"} {"xingming":"张三"} {"xingming":"张三"} {"xingming":"张三"} {"xingming":"张三"}
JsonProperty还有其他参数,此处不做演示讲解,有兴趣可自行测试。
JsonProperty给予的名字,会同时作为序列化和反序列化的名字,即该字段失去了原有的名字。
JsonProperty只能设置一个名字。
如果json字符串中同时含有JsonProperty和JsonAlias给予的名字,反序列化时会以JsonProperty的值为准。 编程切勿纸上谈兵,一切代码都需要自己去实践和感受,才能真正建立自己的理解。
如果使用 @JsonProperty
后,反序列化出现两个key,如:
java@JsonProperty("NAME")
private String NAME;
反序列化时,可能出现NAME
,和name
两个字段,如:
json{
"NAME":"张三",
"name":"张三"
}
我是通过在类上增加注解@JsonAutoDetect(getterVisibility=JsonAutoDetect.Visibility.NONE)
解决该问题的,如:
java@JsonAutoDetect(getterVisibility=JsonAutoDetect.Visibility.NONE)
public class Test{
}
如果你觉得本文对你有用,想要给作者一些赞助,可以动动小手点击下方广告给予支持,万分感谢~
您的每次点击都能给予作者更多分享的动力!请无情点击吧:
本文作者:DingDangDog
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!