@JsonProperty
这个注解提供了序列化和反序列化过程中该java属性所对应的名称
@JsonAlias
这个注解只只在反序列化时起作用,指定该java属性可以接受的更多名称
??public static void main (String[] args ) throws IOException { ???????String a ="{\"NaMe\":\"hello\"}"; ???????ObjectMapper objectMapper = new ObjectMapper(); ???????Label label = objectMapper.readValue(a, Label.class); ???????String labelString = objectMapper.writeValueAsString(label); ???????System.out.println(labelString); ???} ???public static class Label{ ???????//反序列化时两个都可用,都没有会报错 ???????//@JsonAlias("NaMe") ???????@JsonProperty("NaMe") ???????public String name; ???????public Label(){ ???????} ???}
使用@JsonProperty时,序列化结果为:{“NaMe”:“hello”}
使用@JsonAlias时,序列化结果为:{“name”:“hello”}
参考:https://www.concretepage.com/jackson-api/jackson-jsonproperty-and-jsonalias-example.
@JsonProperty和@JsonAlias的区别
原文地址:https://www.cnblogs.com/softidea/p/10445160.html