4

how do i create partial representation from myBean

class MyBean{ A, B, C }

i would like to create csv only for A,B properties

using the following code throw the exception bellow

CsvSchema schema = CsvSchema.builder().
                    .addColumn("A")
                    .addColumn("B")
                    .build();

CsvMapper mapper = new CsvMapper(); ObjectWriter writer = mapper.writer(schema);

byte[] csv = writer.writeValueAsBytes(bean);

getting the exception :

JsonMappingException: Unrecognized column 'C': known columns {"A","B"}

i'm using jackson 2.7.3

Thanks

Z.H.
  • 51
  • 1
  • 4
  • 1
    Possible duplicate of [Ignoring new fields on JSON objects using Jackson](https://stackoverflow.com/questions/5455014/ignoring-new-fields-on-json-objects-using-jackson) – Aaron Jul 13 '17 at 09:10
  • did you take a look at JsonView in Jackson? I had similar question a few days ago, I hope this helps : https://stackoverflow.com/questions/45004348/jackson-remove-some-values-from-json-and-keep-some-null-values – Seyed Ali Roshan Jul 13 '17 at 09:12

2 Answers2

8

Configure CSV Mapper like this

 CsvMapper mapper = new CsvMapper(); 
 mapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN,true);
M1r1
  • 197
  • 2
  • 10
3

Annotate your class with @JsonIgnoreProperties(ignoreUnknown = true)

@JsonIgnoreProperties(ignoreUnknown = true)
class MyBean{ A, B, C }
mlecz
  • 985
  • 11
  • 19