I am using the Jackson JSON library to serialize an object, but the JSON string produced by the ObjectMapper has an extra field that is a duplicate of another field of the class with a different name. I can't figure out where this extra field is coming from and it messes up de-serialization on the other end.
Object Properties
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private int requirement_id;
@JsonProperty("student_uid")
private String student;
private String status;
@Nullable
@JsonProperty("doc_guid")
private String doc_guid;
@Nullable
@JsonProperty("retake_date")
private Date retake_date;
Serialization Code
@RequestMapping(path = "/student/requirements/{uid}")
public String getStudentRequirements(@PathVariable String uid) throws JsonProcessingException
{
List<RequirementInstance> requirements = instance_repo.findByStudent(uid);
if(requirements != null)
{
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
return mapper.writeValueAsString(requirements.toArray());
}
return "{\"error\": \"Unable to locate user with UID of " + uid + ".\"}";
}
Returned JSON String
[{ "id": 1, "status": "Incomplete", "studentUID": "6e8d6c69af58e73c7248364aa59b0c257f6ba1d19782eb9e38890a61ada948ef", "docGUID": null, "requirementID": 1, "retakeDate": null, "student_uid": "6e8d6c69af58e73c7248364aa59b0c257f6ba1d19782eb9e38890a61ada948ef", "doc_guid": null, "retake_date": null }, { "id": 2, "status": "Incomplete", "studentUID": "6e8d6c69af58e73c7248364aa59b0c257f6ba1d19782eb9e38890a61ada948ef", "docGUID": null, "requirementID": 2, "retakeDate": null, "student_uid": "6e8d6c69af58e73c7248364aa59b0c257f6ba1d19782eb9e38890a61ada948ef", "doc_guid": null, "retake_date": null }, { "id": 3, "status": "Incomplete", "studentUID": "6e8d6c69af58e73c7248364aa59b0c257f6ba1d19782eb9e38890a61ada948ef", "docGUID": null, "requirementID": 3, "retakeDate": null, "student_uid": "6e8d6c69af58e73c7248364aa59b0c257f6ba1d19782eb9e38890a61ada948ef", "doc_guid": null, "retake_date": null }]
results from incompatible field/getter/setter naming -- fields would imply properties Ref : https://github.com/FasterXML/jackson-databind/issues/1609 Verify your getter setters are not mapped property / automatically. Because of this you are seeing duplicate. Refer to teh above link, how to solve it.
Or use property naming strategy in ObjectMapper. Ref : https://javadeveloperzone.com/spring/spring-jackson-property-naming-strategy/