杰克逊多态反序列化失败:不同的类,相同的代码

时间:2016-06-11 12:49:32

标签: java json jackson

我有三个类:Request(抽象),RequestCreateContract,RequestUpdateContract。奇怪的是,RequestUpdateContract无法反序列化。我曾尝试将RequestCreateContract代码复制粘贴到RequestUpdateContract中,并且它仍然失败:只更改了类名和构造函数名称。另一方面,RequestCreateContract没有任何问题。

报告的例外:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "contract" (class mypackage.net.protocol.request.RequestUpdateContract), not marked as ignorable (one known property: "login"])

这是代码。 请求(超类)

package mypackage.net.protocol.request;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import mypackage.net.protocol.Login;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
    @JsonSubTypes.Type(value = RequestUpdateContract.class, name = "RequestUpdateContract"),
    @JsonSubTypes.Type(value = RequestCreateContract.class, name = "RequestCreateContract"),
    @JsonSubTypes.Type(value = RequestDeleteContract.class, name = "RequestDeleteContract"),
    @JsonSubTypes.Type(value = RequestFindContract.class, name = "RequestFindContract"),
    @JsonSubTypes.Type(value = RequestGetContract.class, name = "RequestGetContract")


}
)
public abstract class Request {


    protected Login login;

    @JsonCreator
    public Request(@JsonProperty("login") Login login){

        this.login=login;
    }

    public Request(){

    }

    @JsonProperty("login")
    public Login login(){

        return login;
    }

}

RequestCreateContract

package mypackage.net.protocol.request;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import mypackage.datamodel.contract.Contract;
import mypackage.datamodel.contract.dealinfo.DealInfo;
import mypackage.net.protocol.Login;


public class RequestCreateContract  extends Request{

    /*
    DealInfo dealInfo;

    @JsonCreator
    public RequestCreateContract(
            @JsonProperty("login") Login login,
            @JsonProperty("dealinfo") DealInfo dealInfo){

        super(login);
        this.dealInfo=dealInfo;
    }

    @JsonProperty("dealinfo")
    public DealInfo dealInfo(){

        return dealInfo;
    }
    */

    Contract contract;

    @JsonCreator
    public RequestCreateContract(

            @JsonProperty("contract") Contract contract,
            @JsonProperty("login") Login login){

        super(login);
        this.contract=contract;
    }

    @JsonProperty("contract")
    public Contract contract(){

        return contract;
    }

}

RequestUpdateContract

package mypackage.net.protocol.request;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import mypackage.datamodel.contract.Contract;
import mypackage.datamodel.contract.dealinfo.DealInfo;
import mypackage.net.protocol.Login;


public class RequestUpdateContract extends Request{

    /*
    DealInfo dealInfo;

    @JsonCreator
    public RequestCreateContract(
            @JsonProperty("login") Login login,
            @JsonProperty("dealinfo") DealInfo dealInfo){

        super(login);
        this.dealInfo=dealInfo;
    }

    @JsonProperty("dealinfo")
    public DealInfo dealInfo(){

        return dealInfo;
    }
    */

    Contract contract;

    @JsonCreator
    public RequestUpdateContract(

            @JsonProperty("contract") Contract contract,
            @JsonProperty("login") Login login){

        super(login);
        this.contract=contract;
    }

    @JsonProperty("contract")
    public Contract contract(){

        return contract;
    }

}

我希望你能重现这个问题。这些类是客户端和服务器应用程序之间共享项目的一部分。

1 个答案:

答案 0 :(得分:0)

这是一个同步问题,事实证明,通过重构类名将名称更改为RequestUpdateContract会导致另一个错误(Jackson无法解析新的类名)。