Java Deserialization no String-argument constructor/factory method to deserialize from String value

Malena T Source

I would like to send parent key that already exists in the db as a String in the field that is a UserProfile object. I keep getting below error even I set @JsonCreator.

Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.restful.pojo.UserProfile` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('background'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.restful.pojo.UserProfile` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('background')
 at [Source: (PushbackInputStream); line: 2, column: 16] (through reference chain: com.restful.pojo.UserProfile["parentKey"])

Pojo:

@Entity
@Table(name="user_profiles")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class UserProfile implements Serializable {

    private static final long serialVersionUID = -4686534469583600032L;
    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(columnDefinition = "BINARY(16)")
    private UUID id;
    @Column(name="`key`")
    private String key;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_key")
    @JsonBackReference(value="parent-key")
    private UserProfile parentKey;

    @JsonCreator
    public UserProfile(
            @JsonProperty("parentKey") UserProfile parentKey) {
        this.parentKey = parentKey;
    }

    public UserProfile() {
    }

    ... getters/setters
}

JSON:

{
  "key": "opacity",
  "parentKey" :"background"
}

Relation in the db:

.....
`key` VARCHAR(255) NOT NULL,
parent_key VARCHAR(255) DEFAULT NULL,
UNIQUE KEY (`key`),
CONSTRAINT fk_parent_key FOREIGN KEY (parent_key) REFERENCES user_profiles (`key`) ON DELETE NO ACTION ON UPDATE NO ACTION,
....

Is there a way to define parentKey object as a String value?

javahibernatespring-bootjacksondeserialization

Answers

comments powered by Disqus