title: How to dynamically assign headers to a csv file using CsvMapper in Java
body: <p>Can anyone help please?
  I am stuck on reading a csv file and serializing it onto a POJO.
  I am using CsvMapper from jackson library. The reading and serialization part are done and works fine-ish. The issue is when the user moves the headers/columns around causing the serialization to make some alphabetical assumption that the values on the CSV file are also alphabetically.<br>
  Eg (File below has headers on the first row and the second row has person details values)
  <code>
  personNameHeader,personAgeHeader
  Wiliam,32
  </code></p>
  
  <p>Now my POJO is as follow</p>
  
  <pre><code>@JsonIgnoreProperties(ignoreUnknown = true)
  // @JsonPropertyOrder(value = {"personNameHeader", "personAgeHeader" })
  public class PersonDetailsCSVTemplate  {
  
  @JsonProperty("personNameHeader")
  private String name;
  
  @JsonProperty("personAgeHeader")
  private String age;
  
  //Public constructor and getters and setters...
  </code></pre>
  
  <p>This is the code to read the values from the CSV and map onto the class</p>
  
  <pre><code>import com.fasterxml.jackson.databind.MappingIterator;
  import com.fasterxml.jackson.dataformat.csv.CsvMapper;
  import com.fasterxml.jackson.dataformat.csv.CsvSchema;
  ...
  
      CsvMapper csvMapper = new CsvMapper();    
      CsvSchema schema = csvMapper.typedSchemaFor(PersonDetailsCSVTemplate.class).withHeader();
            MappingIterator&lt;PersonDetailsCSVTemplate  &gt; dataIterator = csvMapper.readerFor(PersonDetailsCSVTemplate.class).with(schema)
              .readValues(data);
  
       while (dataIterator.hasNextValue()) {
          PersonDetailsCSVTemplate dataCSV = dataIterator.nextValue();
  }
  </code></pre>
  
  <p>After the serialization it can be seen that CsvMapper mapped the following:
  <code>PersonDetailsCSVTemplate.name = "32"</code> and<code>PersonDetailsCSVTemplate.age = "Wiliam"</code>    </p>
  
  <p>By annotating the class with <code>@JsonPropertyOrder(value = {"personNameHeader", "personAgeHeader" })</code> forces the CSV to be always name column followed by age column which isnt ideal.</p>
  
  <p>Can anyone suggest anything that they think will work?
  Regards</p>