Java反序列化没有用于从String值反序列化的String-argument构造函数/工厂方法

时间:2018-03-14 10:30:45

标签: java hibernate spring-boot jackson deserialization

我想在作为String对象的字段中将已存在于数据库中的父密钥作为UserProfile发送。即使我设置@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"
}

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

有没有办法将parentKey对象定义为String值?

0 个答案:

没有答案