dropwizard框架默认使用的fastxml的jackson注解
一、两种情况下都有效的注解
1. @JsonIgnore
@JsonIgnore 用来告诉 Jackson 在处理时忽略该注解标注的 java pojo 属性,不管是将 java 对象转换成 json 字符串,还是将 json 字符串转换成 java 对象。下面是一个具体的例子
首先定义一个java pojo类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package org.lifw.jackosn.annotation; import com.fasterxml.jackson.annotation.JsonIgnore; public class SomeEntity { private String name; @JsonIgnore private String desc; public String getName() { return name; } public void setName(String name) { this .name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this .desc = desc; } @Override public String toString() { return "SomeEntity [name=" + name + ", desc=" + desc + "]" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package org.lifw.jackosn.annotation; import java.io.IOException; import org.junit.Test; import com.fasterxml.jackson.databind.ObjectMapper; public class AnnotationTest { private String jsonString = "{\"name\":\"aaa\",\"desc\":\"aaa is a good person\"}" ; @Test public void testJonsIgnore() throws IOException { //java对象 -> json字符串 SomeEntity entity = new SomeEntity(); entity.setName( "aaa" ); entity.setDesc( "aaa is a good person" ); System.out.println( new ObjectMapper().writeValueAsString(entity)); //json字符串 -> java对象 SomeEntity entity2 = new ObjectMapper().readValue(jsonString, SomeEntity. class ); System.out.println(entity2); } } |
输出结果
1 2 | { "name" : "aaa" } SomeEntity [name=aaa, desc= null ] |
从上面的例子可以看出序列化和反序列化的过程中 desc 属性都被忽略了,这个注解非常常用。
2. @JsonIgnoreProperties
@JsonIgnoreProperties 和 @JsonIgnore 的作用相同,都是告诉 Jackson 该忽略哪些属性,不同之处是 @JsonIgnoreProperties 是类级别的,并且可以同时指定多个属性。这里采用和上面同样的例子,修改java pojo 如下
1 2 3 4 5 6 7 8 9 10 | package org.lifw.jackosn.annotation; com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties (value={ "desc" }) public class SomeEntity { private String name; @JsonIgnore private String desc; //getter,setter 等省略 } |
运行测试代码,结果和上面的一样。
3. @JsonIgnoreType
@JsonIgnoreType 标注在类上,当其他类有该类作为属性时,该属性将被忽略。
1 2 3 4 5 6 7 8 9 10 11 12 | package org.lifw.jackosn.annotation; import com.fasterxml.jackson.annotation.JsonIgnoreType; @JsonIgnoreType public class SomeOtherEntity { private Long id; public Long getId() { return id; } public void setId(Long id) { this .id = id; } } |
1 2 3 4 5 | public class SomeEntity { private String name; private String desc; private SomeOtherEntity entity; } |
SomeEntity 中的 entity 属性在json处理时会被忽略。
4. @JsonProperty
@JsonProperty 可以指定某个属性和json映射的名称。例如我们有个json字符串为{"user_name":"aaa"},而java中命名要遵循驼峰规则,则为userName,这时通过@JsonProperty 注解来指定两者的映射规则即可。这个注解也比较常用。
1 2 3 4 5 6 7 | import com.fasterxml.jackson.annotation.JsonProperty; public class SomeEntity { @JsonProperty ( "user_name" ) private String userName; // ... } |
二、只在序列化情况下生效的注解
1. @JsonPropertyOrder
在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonPropertyOrder 可以指定属性在 json 字符串中的顺序。
1 2 3 4 5 6 7 8 | import com.fasterxml.jackson.annotation.JsonPropertyOrder; @JsonPropertyOrder (value={ "desc" , "name" }) public class SomeEntity { private String name; private String desc; //.. } |
2. @JsonInclude
在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonInclude 注解可以控制在哪些情况下才将被注解的属性转换成 json,例如只有属性不为 null 时。
1 2 3 4 5 6 7 | import com.fasterxml.jackson.annotation.JsonInclude; @JsonInclude (JsonInclude.Include.NON_NULL) public class SomeEntity { private String name; private String desc; } |
与之对应的老版本使用:
@JsonSerialize(include = JsonSerialize.Inclusion.ALWAYS)
上述例子的意思是 SomeEntity 的所有属性只有在不为 null 的时候才被转换成 json,如果为 null 就被忽略。该注解也可以加在某个字段上。另外还有很多其它的范围,例如 NON_EMPTY、NON_DEFAULT等,这些在不同版本的 jackson 中含义不同,具体请参考 jackson api,比较混乱。
三、是在反序列化情况下生效的注解
1. @JsonSetter
@JsonSetter 标注于 setter 方法上,类似 @JsonProperty ,也可以解决 json 键名称和 java pojo 字段名称不匹配的问题。
1 2 3 4 5 6 7 8 9 | import com.fasterxml.jackson.annotation.JsonSetter; public class SomeEntity { private String desc; @JsonSetter ( "description" ) public void setDesc(String desc) { this .desc = desc; } } |