码迷,mamicode.com
首页 > Web开发 > 详细

@JsonProperty和@JsonAlias的区别

时间:2019-02-27 17:39:24      阅读:1576      评论:0      收藏:0      [点我收藏+]

标签:注解   tps   new   value   nal   oid   ring   序列   stat   

@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的区别

标签:注解   tps   new   value   nal   oid   ring   序列   stat   

原文地址:https://www.cnblogs.com/softidea/p/10445160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!