本文介绍了自更新至2.8.1以来,Jackson尝试使用所有参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我刚刚将Spring Boot从1.3.2更新到了1.4,结果杰克逊也从2.6.5更新到了2.8.1.

I just updated spring boot from 1.3.2 to 1.4 and as a result also updated jackson from 2.6.5 to 2.8.1.

在代码没有其他更改的情况下,杰克逊现在尝试使用all-args-constructor(AnnotatedConstructor.java:124)而不是常规的no-args-constructor(AnnotatedConstructor.java:120)创建我的模型类的实例.

With no other change in code jackson now tries to create instances of my model class using an all-args-constructor (AnnotatedConstructor.java:124) instead of the normal no-args-constructor (AnnotatedConstructor.java:120).

由于杰克逊使用的构造器顺序错误(将String设置为Long),这将导致异常:

Since jackson is using the constructor in the wrong order (setting a String for a Long) this results in an exception:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.example.PoJO, problem: argument type mismatch
at [Source: java.io.PushbackInputStream@52de93f1; line: 1, column: 1138]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:268)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1405)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapAsJsonMappingException(StdValueInstantiator.java:468)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.rewrapCtorProblem(StdValueInstantiator.java:487)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:276)
at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:224)
at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:135)
at com.fasterxml.jackson.databind.deser.impl.ExternalTypeHandler.complete(ExternalTypeHandler.java:225)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeUsingPropertyBasedWithExternalTypeId(BeanDeserializer.java:937)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeWithExternalTypeId(BeanDeserializer.java:792)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:312)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2913)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:225)
... 52 common frames omitted
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:124)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:274)
... 62 common frames omitted

由于我没有更改代码,所以我不知道从哪里开始调试.我验证了将杰克逊修复到2.6.5可以解决此问题.

Since I made no change to my code I have no idea where to begin debugging. I verified that fixing jackson back to 2.6.5 resolves this problem.

为什么杰克逊尝试使用all-args-constructor,并且有办法强制使用no-args-constructor?

why does jackson try to use the all-args-constructor and is there a way to force the use of the no-args-constructor?

推荐答案

我不得不混合使用以下答案:

I had to use a mix of this answer : how to specify jackson to only use fields - preferably globally

并感谢 @StaxMan 的评论:


在要反序列化的对象上,删除自动JsonCreator自动检测:


On the object to deserialize, remove auto JsonCreator auto detection :

@Value
@AllArgsConstructor
// Add a private default constructor used through reflection
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
// Deactivate JsonCreator auto detection
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, creatorVisibility = Visibility.NONE)
public class ValueObject {

    @NotEmpty
    private String x;
}

Lombok的@AllArgsConstructor不允许您再覆盖suppressConstructorProperties.

Lombok's @AllArgsConstructor does not allow you to override suppressConstructorProperties anymore.

在您的lombok.config文件中,添加:

lombok.anyConstructor.suppressConstructorProperties=true

这篇关于自更新至2.8.1以来,Jackson尝试使用所有参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:57