杰克逊映射到错误的对象

时间:2015-11-18 10:57:40

标签: java json jersey jackson

我有一个类型为Troubleshoot的JSON对象,我想将其存储在后端。该对象的一个​​参数是问题列表

QuestionList: Array[1]
    0: Object
        id: 1116
        question: "Question 1"
        update: 1447842794620

它应该映射到后端

中的Question对象
@XmlRootElement
public class Question extends AbstractNodeUpdateProperty
{

   private long mId;
   private String mQuestion;

   /**
    * No-arg constructor required for JaxB serialization.
    */
   public Question()
   {
      super();
   }

   public Question(long aId, String aQuestion, long aUpdate)
   {
      super(aUpdate);
      mId = aId;
      mQuestion = aQuestion;
   }

   @XmlElement
   public long getId()
   {
      return mId;
   }

   @XmlElement
   public String getQuestion()
   {
      return mQuestion;
   }

   public void setId(long aId)
   {
      mId = aId;
   }

   public void setQuestion(String aQuestion)
   {
      mQuestion = aQuestion;
   }

   @Override
   public String toString()
   {
      return "Id: " + mId + ", question: " + mQuestion;
   }
}

但相反,杰克逊似乎希望它将其映射到Suggestion对象

@XmlRootElement
public class Suggestion extends AbstractNodeUpdateProperty
{
   private long mId;
   private String mTitle;
   private String mActionToTake;
   private Boolean mIsIntegrated;

   /**
    * No-arg constructor required for JaxB serialization.
    */
   public Suggestion()
   {
      super();
   }

   public Suggestion(long aId, String aTitle, String aActionToTake, long aUpdate)
   {
      super(aUpdate);
      mId = aId;
      mTitle = aTitle;
      mActionToTake = aActionToTake;
      mIsIntegrated = true;
   }

   public Suggestion(long aId, String aTitle, String aActionToTake, long aUpdate, Boolean aIsIntegrated)
   {
      super(aUpdate);
      mId = aId;
      mTitle = aTitle;
      mActionToTake = aActionToTake;
      mIsIntegrated = aIsIntegrated;
   }

   @XmlElement
   public long getId()
   {
      return mId;
   }

   @XmlElement
   public String getTitle()
   {
      return mTitle;
   }

   @XmlElement
   public String getActionToTake()
   {
      return mActionToTake;
   }

   @XmlElement
   public Boolean getIsIntegrated()
   {
      return mIsIntegrated;
   }

   public void setId(long aId)
   {
      mId = aId;
   }

   public void setTitle(String aTitle)
   {
      mTitle = aTitle;
   }

   public void setActionToTake(String aActionToTake)
   {
      mActionToTake = aActionToTake;
   }

   public void setIsIntegrated(Boolean pIsIntegrated)
   {
      mIsIntegrated = pIsIntegrated;
   }
}

这是我收到的错误:

nov 18, 2015 11:41:30 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "question" (Class com.iba.smi.troubleshooting.suggestion.Suggestion), not marked as ignorable
 at [Source: org.apache.catalina.connector.CoyoteInputStream@28fe95ed; line: 1, column: 203] (through reference chain: com.iba.smi.troubleshooting.TroubleshootResult["Troubleshoot"]->com.iba.smi.troubleshooting.troubleshoot.Troubleshoot["QuestionList"]->com.iba.smi.troubleshooting.suggestion.Suggestion["question"])

我是否正确解释了这个错误?知道为什么会出现这种错误以及如何解决它?

故障排除课程 所有参数都有getter和setter,但为了长度我稍微缩短了一点:

@XmlRootElement
public class Troubleshoot extends AbstractNodeUpdateProperty
{
   private String mAlarmId;
   private ArrayList<Suggestion> mSuggestionList = new ArrayList<>();
   private ArrayList<Question> mQuestionList = new ArrayList<>();
   private ArrayList<AlarmLink> mAlarmLinkList = new ArrayList<>();

   private ArrayList<SuggestionAskQuestion> mSuggestionAskQuestionList = new ArrayList<>();
   private ArrayList<SuggestionAskAlarmLink> mSuggestionAskAlarmLinkList = new ArrayList<>();
   private ArrayList<AlarmAskQuestion> mAlarmAskQuestionList = new ArrayList<>();
   private ArrayList<AlarmAskSuggestion> mAlarmAskSuggestionList = new ArrayList<>();
   private ArrayList<AlarmAskAlarmLink> mAlarmAskAlarmLinkList = new ArrayList<>();
   private ArrayList<QuestionAnswerSuggestion> mQuestionAnswerSuggestionList = new ArrayList<>();
   private ArrayList<QuestionAnswerAlarmLink> mQuestionAnswerAlarmLinkList = new ArrayList<>();

   /**
    * No-arg constructor required for JaxB serialization.
    */
   public Troubleshoot()
   {
      super();
   }

   public Troubleshoot(String aAlarmId, long aUpdate)
   {
      super(aUpdate);
      mAlarmId = aAlarmId;
   }

   @XmlElement(name = "SuggestionList")
   public ArrayList<Suggestion> getSuggestionList()
   {
      return mSuggestionList;
   }

   @XmlElement(name = "QuestionList")
   public ArrayList<Question> getQuestionList()
   {
      return mQuestionList;
   }

   public void setSuggestionList(ArrayList<Suggestion> aSuggestionList)
   {
      this.mSuggestionList = aSuggestionList;
   }

   public void setQuestionList(ArrayList<Suggestion> aQuestionList)
   {
      this.mSuggestionList = aQuestionList;
   }
}

0 个答案:

没有答案