Simple string-argument constructor not found if @ConstructorProperties and getter are present
See original GitHub issueUpgrading to 2.7.6, I’ve hit a bug that looks like it was introduced in 2.7.0. It doesn’t happen in 2.6.x. Simple wrapper classes for String values used to be deserializable from JSON string values in 2.6, but starting in 2.7 an exception is thrown if there’s both a @ConstructorProperties
annotation on the constructor and a bean-style getter. Example code:
public class JacksonTest {
@Test
public void testConstructor() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.readValue("\"foobar\"", StringWrapper.class);
}
public static class StringWrapper {
private final String value;
@java.beans.ConstructorProperties({"value"})
public StringWrapper(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
}
}
When I run this under 2.7 or higher (tried 2.7.0, 2.7.6, and 2.8.0) I get an error:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of example.JacksonTest$StringWrapper: no String-argument constructor/factory method to deserialize from String value ('foobar')
at [Source: "foobar"; line: 1, column: 1]
Removing either the getter or the annotation on the constructor fixes the problem.
In our actual non-example code, we’re using Lombok to generate constructors and getters, so this might be the same as issue #1239. But the problem still occurs when Lombok isn’t involved and we’re not using any mixins like that issue describes, so I’m filing this as its own issue rather than commenting on that one.
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
with jdk8, I manage to get it work with this : @AllArgsConstructor(onConstructor=@__({@JsonCreator(mode = JsonCreator.Mode.DELEGATING)}))
I got similar error, but this time it is due to the removal of
@ConstructorProperties
in the code. Lombok don’t generate those annotations by default to help the migration to Java 9 (see https://github.com/rzwitserloot/lombok/issues/1563). (java.beans.ConstructorProperties
belongs to thejava.desktop
module). Adding either@JsonCreator
on the constructor@JsonProperty
did however removed the problem.