Skip to content
Advertisement

JavaJSON: UnrecognizedPropertyException: Unrecognized field “CefMessagesGenerators” error

I have the following Json and corresponding Java classes.

Could you please check it out and advise why do I get this exception?

Is it because I added another


public class CefMessagesGenerator {

    private String ip;
    private String username;
    private String password;

    //getters and setters    
}


public class CefMessagesGenerators {

    private List<CefMessagesGenerator> cefMessagesGeneratorList = null;

    public CefMessagesGenerators() {
    }

    public CefMessagesGenerators(List<CefMessagesGenerator> cefMessagesGeneratorList) {
        super();
        this.cefMessagesGeneratorList = cefMessagesGeneratorList;
    }

    public List<CefMessagesGenerator> getCefMessagesGeneratorList() {
        return cefMessagesGeneratorList;
    }

    public void setCefMessagesGeneratorList(List<CefMessagesGenerator> cefMessagesGeneratorList) {
        this.cefMessagesGeneratorList = cefMessagesGeneratorList;
    }
}

public class ControllerLab {

    private KubernetesCluster kubernetesCluster;
    private AzureEnvironment azureEnvironment;
    private PortalEnv portalEnv;
    private List<CefMessagesGenerator> cefMessagesGenerators = null;

    //getters and setters

    public List<CefMessagesGenerator> getCefMessagesGenerators() {
        return cefMessagesGenerators;
    }

    public void setCefMessagesGenerators(List<CefMessagesGenerator> cefMessagesGenerators) {
        this.cefMessagesGenerators = cefMessagesGenerators;
    }
}

and the (partial) json is:

enter image description here

(Unfortunately I had to add the json image instead as text here because the system claims “I have the following Json and corresponding Java classes.”…)

Advertisement

Answer

As already described in the comments the main problem would be that your json contains the property name CefMessagesGenerators but the class ControllerLab contains the property cefMessagesGenerators (note the case difference for the first letter). Thus The parser can’t find the property “CefMessagesGenerators”.

To solve this there would be a couple of options, depending on which parser you’re using:

  • Rename the json property to “cefMessagesGenerators” (preferred option)
  • Define a non-standard propertyname in your Pojo (e.g. via some annotation like Jackson’s @JsonProperty)
  • Define a custom naming strategy that allows the parser to map CefMessagesGenerators to cefMessagesGenerators

Note that the first option would be the preferred one for multiple reasons. One of the more important reasons would be that by doing so you wouldn’t violate the principle of least astonishment (people would expect the property names to match exactly).

Finally, to quote your comment:

I changed the json label to start with small letter and left the class name to begin with a capital letter and it worked (don’t understand why…)

Note that the class CefMessagesGenerators is not relevant here since it is not used by ControllerLab. Thus the class name doesn’t matter at all.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement