尝试将@JsonCreator
与@JsonProperty(value = "version", access = JsonProperty.Access.READ_ONLY)
沿着使用时出现InvalidDefinitionException
这是我的班级的样子:
@Entity
public class Example1 implements Serializable {
private String field1;
private int field2;
private Example2 field3;
public Example1(@JsonProperty(value = "field_1", access = JsonProperty.Access.READ_WRITE) String field1,
@JsonProperty(value = "field_2", access = JsonProperty.Access.READ_ONLY) int field2,
@JsonProperty(value = "field_3_1", access = JsonProperty.Access.READ_WRITE) String field31,
@JsonProperty(value = "field_3_2", access = JsonProperty.Access.READ_WRITE) int field32
) {
this.field1 = field1;
this.field2 = field2;
this.field3 = new Example2(field31, field32);
}
// getters and setters omitted
}
它抛出以下异常:
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Argument #1 of constructor [constructor for com.test.example.model.Example1, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)}] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator
at [Source: (File); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:62) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:249) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:165) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:411) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:349) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:477) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4145) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3995) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2878) ~[jackson-databind-2.9.0.pr3.jar:2.9.0.pr3]
我得环境详细信息:我使用的是Spring Boot 2.0.0.M2
版本和Jackson 2.9.0.pr3
。
我的意图是在反序列化期间省略*******字段2。
我尝试在field2的setter方法中使用@JsonIgnore
作为替代方法。甚至,它不适用于@JsonCreator
。
1条答案
按热度按时间vngu2lb81#
在反序列化时,有两种方法可以忽略该属性
1.使用只读
只需向不想反序列化的属性添加一个注解
1.使用@JsonIgnore和@JsonProperty
这个方法应该小心,你应该把@JsonIgnore放在setter之前,把@JsonProperty放在getter之前,并且对于这个属性应该清除,不带任何注解。
最后,给出了@JsonIgnore、@JsonProperty、@JsonProperty(access = xxx)的完整注解示例。
bean的定义
测试JSON
反序列化的结果:如果该值为null,则不对该值进行反序列化。