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.

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).

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

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.

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

Laures
  • 5,389
  • 11
  • 50
  • 76
  • 1
    Are you by any chance using Lombok? If so, it tends to add `@ConstructorProperties` by default, which is interpreted by Jackson similar to `@JsonCreator`, suggesting that the annotated constructor should indeed be used. But that behavior can be configured, to avoid adding that annotation. – StaxMan Sep 11 '16 at 04:18

2 Answers2

3

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

AND thanks to @StaxMan's comment :

Are you by any chance using Lombok?
If so, it tends to add @ConstructorProperties by default, which is interpreted by Jackson similar to @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's @AllArgsConstructor does not allow you to override suppressConstructorProperties anymore.

In your lombok.config file, add :

lombok.anyConstructor.suppressConstructorProperties=true
Michael Técourt
  • 3,457
  • 1
  • 28
  • 45
0

Looking into Jackson annotations, I guess you could use no args constructor explicitly:

public class YourPojo {
   @JsonCreator
   public YourPojo(){}
}
luboskrnac
  • 23,973
  • 10
  • 81
  • 92