The Question
Lombok's @builder
annotation creates a builder-class for a class. To support deserialization of json items (using Jackson's ObjectMapper), I've added the following annotations:
@Builder
@JsonDeserialize(builder = Item.ItemBuilder.class)
@JsonPOJOBuilder(withPrefix="")
public class Item {
@Getter
String partitionvalue;
}
This is based on the @Jacksonized
documentation. On the usage of the deserializer, upon a json file which is stored in AWS S3 bucket and its content is simply: {"partitionvalue": "test"}
, my code is:
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.withRegion(region)
.build();
S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucket, key));
Item item = objectMapper.readValue(s3Object.getObjectContent(), Item.class);
However when running on a json file that Jackson fails with the message:
Unrecognized field "partitionvalue" (class com.example.Test$TestBuilder),
not marked as ignorable (0 known properties: ])
at [Source: com.amazonaws.services.s3.model.S3ObjectInputStream@2ca47471; line: 1, column: 21] (through reference chain: com.example.TestBuilder["partitionvalue"])
Extra Details
Using @
Jacksonized
annotation directly didn't work as well, and since it is lombok-experimental I used the annotations I needed to use with builder directly.I verified that Lombok will do what I expect of a builder class by using "delombok" option in the Lombok IntelliJ plugin:
public class Item {
String partitionvalue;
Item(String partitionvalue) {
this.partitionvalue = partitionvalue;
}
public static ItemBuilder builder() {
return new ItemBuilder();
}
public String getPartitionvalue() {
return this.partitionvalue;
}
public static class ItemBuilder {
private String partitionvalue;
ItemBuilder() { }
public Item.ItemBuilder partitionvalue(String partitionvalue) {
this.partitionvalue = partitionvalue;
return this;
}
public Item build() {
return new Item(partitionvalue);
}
public String toString() {
return "Item.ItemBuilder(partitionvalue=" + this.partitionvalue + ")";
}
}
}
- Without the
@Builder
annotation (and with adding@NoArgsConstructor
+@AllArgsConstructor
+@Setter
) it worked fine, so the problem isn't with the file from the S3 bucket or the way it is parsed.