Home > Enterprise >  JSON parse error: Unrecognized field MongoDB Spring Boot
JSON parse error: Unrecognized field MongoDB Spring Boot

Time:01-13

I am trying to save the following Java object as a document in MongoDB, here are my classes:

public class GenericIpConfig {
    String connection_type;
    String port;
    String port_ingenico;
    String ip;
    String ip_ingenico;
    String id_ingenico;
    boolean active_ingenico;
    long lastPushTime;
}

@Data
@Document(collection = "generic_device_config")
public class GenericDeviceConfig {
    @Id
    String _id;

    String storeCode;
    String companyCode;

    long updated;
    boolean enabled;

    ArrayList<SerialPort> serialPorts;
    String companyId;

    GenericIpConfig ipConfig;
}

And this is the request I am sending with POSTMAN:

{
    "updated":0,
    "enabled":true,
    "serialPorts":[],
    "companyId":"600",
    "companyCode":"0",
    "storeCode":"0",
    "ipConfig": {
        "connection_type":"ETHERNET",
        "port": "23",
        "port_ingenico": "",
        "ip": "192.168.10.55",
        "ip_ingenico": "",
        "id_ingenico": "",
        "active_ingenico": false,
        "lastPushTime": 0
    }
}

For some reason, I keep getting the following error:

JSON parse error: Unrecognized field \"connection_type\" (class it.igesa.monitoringsystem.model.mongo.device.config.GenericIpConfig), not marked as ignorable; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"connection_type\" (class it.igesa.monitoringsystem.model.mongo.device.config.GenericIpConfig), not marked as ignorable (0 known properties: ]

I feel like my code is missing something, since this is the first time I am working with SpringBoot/MongoDB;

CodePudding user response:

It looks as if Jackson can't recognise setter methods for fields, because they don't follow java naming conventions. Try fixing the unrecognised fields with this annotation - @JsonProperty("property_name_here"), by putting it on the setters.

You are also mixing them, some of your fields are named something_something, while others are somethingSomething, which is the standard java way. It's a good idea to pick one and stick to it for the project.

You should also read up on serialization with jackson documentation, annotation examples.

CodePudding user response:

Finally I found the solution. Defining setters and getters annotations for the nested class solved the problem.

  •  Tags:  
  • Related