No Creators, like default constructor, exist, Cannot deserialize from Object value (no delegate- or property-based Creator)
Asked Answered
G

4

6

I am getting this error for this class while deserializing the data, any input what I am doing wrong here:

Cannot construct instance of `Test` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

 @Getter
@Builder(builderClassName = "Builder")
@JsonIgnoreProperties(ignoreUnknown = true)
  public class Test
{

public Test(String service, String user) {
        this.service = service;
        this.user = user;
    }
   
    private static final long serialVersionUID = 1L;

   
    @JsonProperty("service")
    private String service;

    
    @JsonProperty("user")
    private String user;

    
    @SuppressWarnings("java:S2094")
    public static class Builder
    {
       
    }
}
Garfield answered 22/9, 2022 at 7:44 Comment(3)
try by adding a no param constructor. also provide setters and gettersRamsgate
Looks like you are using Lombok - you should add that tag. And search for "Lombok jackson", you will find plenty of resources. This may also be useful: projectlombok.org/features/experimental/JacksonizedQueenstown
@Queenstown I've confirmed that the code compiles and produces the given error under your assumptions that this is both Lombok & Jackson, so I've made your suggested edits to the question's tags.Milli
M
3

Adding the @Jacksonized Lombok annotation to your class got it working for me:

@Getter
@Builder(builderClassName = "Builder")
@JsonIgnoreProperties(ignoreUnknown = true)
@Jacksonized
public class Test {

Per the Lombok documentation:

The @Jacksonized annotation is an add-on annotation for @Builder and @SuperBuilder. It automatically configures the generated builder class to be used by Jackson's deserialization. It only has an effect if present at a context where there is also a @Builder or a @SuperBuilder; a warning is emitted otherwise.

Without @Jacksonized, you would have to customize your builder class(es). With @Jacksonized, you can simply write something like this to let Jackson use the generated builder:

@Jacksonized @Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class JacksonExample {
  private List<Foo> foos;
}
Milli answered 11/9, 2023 at 22:44 Comment(0)
D
2

Use @ConstructorProperties from Java 8 beans module:

import java.beans.ConstructorProperties;

@ConstructorProperties({"prop1","prop2"})
public CustomClass(String prop1, String prop2) {
  this.prop1 = prop1;
  this.prop2 = prop2;
}
Drifter answered 22/10 at 10:36 Comment(1)
I've confirmed that this works, though obviously the property names need to be adapted for the specific scenario (@ConstructorProperties({"service","user"}))Milli
T
1

Probably won't help this exact case, but since this is the top search result I'll add this in case it's helpful to others:

It turns out interfaces can complicate things and result in this jackson error. This guide helped me: https://andrewtarry.com/posts/deserialising-an-interface-with-jackson/

Trilateral answered 11/9, 2023 at 22:30 Comment(0)
V
0

You have two variants of what to do:

  1. Use @Builder instead of @Builder(builderClassName = "Builder"). Lombok will create the builder name automatically.
  2. Add @NoArgsConstructor and @Setter annotations on the class level.
Varnish answered 11/9, 2023 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.