question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simple string-argument constructor not found if @ConstructorProperties and getter are present

See original GitHub issue

Upgrading 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:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
bfayettecommented, Aug 9, 2017

with jdk8, I manage to get it work with this : @AllArgsConstructor(onConstructor=@__({@JsonCreator(mode = JsonCreator.Mode.DELEGATING)}))

0reactions
bric3commented, Apr 5, 2018

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 the java.desktop module). Adding either

  • a simple @JsonCreator on the constructor
  • or a annotate the constructor parameter with @JsonProperty

did however removed the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Don't understand @ConstructorProperties - Stack Overflow
JavaBeans usually have a public default constructor (with no arguments) and public getter and setter methods for all fields. This means they are ......
Read more >
@NoArgsConstructor, @RequiredArgsConstructor ...
Overview. This set of 3 annotations generate a constructor that will accept 1 parameter for certain fields, and simply assigns this parameter to...
Read more >
ConstructorProperties (Java Platform SE 7 )
An annotation on a constructor that shows how the parameters of that constructor correspond to the constructed object's getter methods. For example:.
Read more >
Lombok Configuration System | Baeldung
The configuration system of Lombok offers us many valuable settings ... ConstructorProperties annotation to all constructors with arguments.
Read more >
Effective Dart: Design
If the member needs no parameters to do that, it should generally be a getter. But sometimes a logical “property” needs some parameters....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found