Search code examples
javarestjersey

How do I fix the "Unrecognized field" problem?


This is my CallTree.java source code:

public class CallTree {
    public static final int active=1;
    public static final int inactive=0;
    private String contact=new String();
    private String division=new String();
    private String systemName=new String();
    private String location=new String();
    private String serviceLevel=new String();
    private String missionCritical=new String();
    private String timeToStartProcedure=new String();
    private String callTree=new String();
    private String timeToEscalate=new String();
    private String logRecipients=new String();
    private int status=CallTree.inactive;   
    private int callTreeId;
    private float version=0.0f;

    private Manual[] manuals;


    public CallTree() {

    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public int getCallTreeId() {
        return callTreeId;
    }

    public void setCallTreeId(int callTreeId) {
        this.callTreeId = callTreeId;
    }
    public String getDivision() {
        return division;
    }

    public void setDivision(String division) {
        this.division = division;
    }

    public String getSystemName() {
        return systemName;
    }

    public void setSystemName(String systemName) {
        this.systemName = systemName;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getServiceLevel() {
        return serviceLevel;
    }

    public void setServiceLevel(String serviceLevel) {
        this.serviceLevel = serviceLevel;
    }

    public String getMissionCritical() {
        return missionCritical;
    }

    public void setMissionCritical(String missionCritical) {
        this.missionCritical = missionCritical;
    }

    public String getTimeToStartProcedure() {
        return timeToStartProcedure;
    }

    public void setTimeToStartProcedure(String timeToStartProcedure) {
        this.timeToStartProcedure = timeToStartProcedure;
    }

    public String getCallTree() {
        return callTree;
    }

    public void setCallTree(String callTree) {
        this.callTree = callTree;
    }

    public String getTimeToEscalate() {
        return timeToEscalate;
    }

    public void setTimeToEscalate(String timeToEscalate) {
        this.timeToEscalate = timeToEscalate;
    }

    public String getLogRecipients() {
        return logRecipients;
    }

    public void setLogRecipients(String logRecipients) {
        this.logRecipients = logRecipients;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Manual[] getManuals() {
        return manuals;
    }

    public void setManuals(Manual[] manuals) {
        this.manuals = manuals;
    }
    public float getVersion() {
        return version;
    }
    public void setVersion(float version) {
        this.version = version;
    }   
}

This is my CallTreeForm.java source code:

    public class CallTreeForm extends CallTree {
        private boolean isNewContact;
        private int contactId;
        private String contact;

        public CallTreeForm() {

        }
        public boolean isNewContact() {
            return isNewContact;
        }
        public void setNewContact(boolean isNewContact) {
            this.isNewContact = isNewContact;
        }
        public String getContact() {
            return contact;
        }
        public void setContact(String contact) {
            this.contact = contact;
        }
        public int getContactId() {
            return contactId;
        }
        public void setContactId(int contactId) {
            this.contactId = contactId;
        }
    }

I submit the following JSON data to the Restful Service.

{
    "division": "A1",
    "systemName": "vdf",
    "location": "dsf",
    "serviceLevel": "2",
    "missionCritical": "M",
    "timeToStartProcedure": "fd",
    "timeToEscalate": "df",
    "logRecipients": "sd@com",
    "status": 0,
    "isNewContact": true,
    "contact": "<p><span style=\"color:#2ecc71\">sdfd</span>f<span style=\"color:#c0392b\">dsfdsfsd</span></p>\n"
}

I got the following error message from the browser.

Unrecognized field "isNewContact" (class com.calltree.CallTreeForm), not marked as ignorable (16 known properties: "contact", "missionCritical", "division", "serviceLevel", "status", "location", "callTreeId", "version", "timeToStartProcedure", "systemName", "contactId", "callTree", "logRecipients", "newContact", "manuals", "timeToEscalate"])
 at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 200] (through reference chain: com.calltree.CallTreeForm["isNewContact"])

How do I fix the "Unrecognized field" problem?


Solution

  • Your isNewContact and its related getter/setter don't follow the standard java bean naming.

    As the property is named isNewContact, the getter is expected to be isIsNewContract, and the setter setIsNewContract.

    But that simply sounds wrong because isNewContact is apparently meant to be the getter name. So I would change the names to:

    private boolean newContact; //property
    public boolean isNewContract(){return this.newContract;}
    public void setNewContract(boolean newContract){this.newContract = newContract;}
    

    And the JSON:

    "newContact": true,