2016-06-11 59 views
0

我有三個類:請求(抽象),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; 
    } 

} 

我希望你能重現該問題。這些類是客戶端和服務器應用程序之間共享項目的一部分。

回答

0

這是一個同步問題,事實證明,通過重構類名將名稱更改爲RequestUpdateContract會導致另一個錯誤(Jackson無法解析新的類名)。