I am trying to parse a CSV and serialize it using the Jackson library. I have tried several methods but can't get to ignore the extra columns in the CSV which are not defined in the POJO. Requirements:
- The columns in the incoming CSV can be in any order.
- There can be some columns which are defined in POJO but not there in CSV (missing columns).
- There can be some columns in the CSV which are not defined in POJO (extra columns).
I have already tried @JsonIgnoreProperties(true)
and also tried to use DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
but nothing seems to work properly.
POJO:
public class student{
@JsonProperty("STUDENT_NAME")
private String name;
@JsonProperty("DOB")
private String dateOfBirth;
@JsonProperty("ID")
private String id;
}
CSV:
STUDENT_NAME,ID,STANDARD,DOB
John,1,4,01/02/2000
Doe,2,5,02/01/1999
I believe the issue is that you need to specify a specific Schema.
For the Student class defined thusly:
This approach appears to work (tested using a String input, but obviously can change to read a file):
Tested via:
Update: I added a test to use a csv file, and it passed the same tests with the one caveat that I needed to set skip empty lines as a blank line at the end of the file was creating an entry with null values.