<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-avro</artifactId>
<version>2.13.0</version>
</dependency>
avro
{
"type": "record",
"name": "User",
"fields": [
{"name": "id", "type": "int"},
{"name": "name", "type": "string"},
{"name": "email", "type": "string"}
]
}
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
import java.io.File;
public class AvroSerializationExample {
public static void main(String[] args) throws Exception {
AvroMapper mapper = new AvroMapper();
User user = new User(1, "John Doe", "john.doe@example.com");
byte[] serializedData = mapper.writerFor(User.class).writeValueAsBytes(user);
File outputFile = new File("user.avro");
mapper.writerFor(User.class).writeValue(outputFile, user);
User deserializedUser = mapper.readerFor(User.class).readValue(serializedData);
System.out.println(deserializedUser);
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.avro.AvroModule;
public class JacksonAvroExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper().registerModule(new AvroModule());
User user = new User(1, "John Doe", "john.doe@example.com");
String jsonString = mapper.writeValueAsString(user);
System.out.println(jsonString);
}
}