Search code examples
javacsvjacksonjackson-dataformat-csv

How to print only the header into a string in Jackson CSVMapper


I am using Jackson CSVMapper to read a tab separated text file into a list of java objects, and write it back into another tab separated text file. My class looks like this:

@Getter
@Setter
@ToString
@NoArgsConstructor
@JsonPropertyOrder({
    "Id",
    "Name",
    "Address 1",
    "Address 2"})
public class InputSchema {
    @JsonProperty("Id")private String id;
    @JsonProperty("Name") private String name;
    @JsonProperty("Address 1") private String address1;
    @JsonProperty("Address 2") private String address2;
}

Parser code:

public List<InputSchema> parseAddresses(String fileContent) throws IOException {
    List<InputSchema> inputAddresses = new ArrayList<>();
    InputStream inputStream = new ByteArrayInputStream(fileContent.getBytes());
    MappingIterator<InputSchema> iterator = getInputSchemaMappingIterator(inputStream);

        while (iterator.hasNextValue()) {
            InputSchema inputAddress = iterator.nextValue();
            log.info(inputAddress.toString());
            inputAddresses.add(inputAddress);
        }

    return inputAddresses;
}

private MappingIterator<InputSchema> getInputSchemaMappingIterator(InputStream inputStream) throws IOException {
    CsvMapper mapper = new CsvMapper();
    mapper.configure(CsvParser.Feature.FAIL_ON_MISSING_COLUMNS, true);
    mapper.configure(CsvParser.Feature.TRIM_SPACES, true);
    CsvSchema schema = mapper.schemaFor(InputSchema.class)
                        .withColumnSeparator('\t')
                        .withHeader()
                        .withStrictHeaders(true);

    ObjectReader reader = mapper.readerFor(InputSchema.class).with(schema);
    MappingIterator<InputSchema> iterator = reader.readValues(inputStream);

    return iterator;
}

Writer code:

private String toTxt(List<InputSchema> inputAddresses) throws JsonProcessingException {
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = mapper.schemaFor(InputSchema.class)
        .withColumnSeparator('\t')
        .withLineSeparator("\n")
        .withHeader()
        .withoutQuoteChar();

    return mapper.writer(schema).writeValueAsString(inputAddresses);
}

I want to extract just the header irrespective of the data. So,

String header = extractHeader();

should return

"Id\tName\tAddress 1\tAddress 2"

How can I do it?

Thanks!


Solution

  • The following worked:

        public static String displayHeader() {
        CsvMapper mapper = new CsvMapper();
    
        JavaType javaType = mapper.getTypeFactory().constructType(InputSchema.class);
        BeanDescription beanDescription = mapper.getSerializationConfig().introspect(javaType);
        List<BeanPropertyDefinition> properties = beanDescription.findProperties();
    
        List<String> columnNames = properties.stream()
            .map(e -> e.getName())
            .collect(Collectors.toList());
        String header = String.join("\t", columnNames);
    
        return header;
    }
    

    reference: @cassiomolin 's answer