in example/src/main/java/org/apache/rocketmq/schema/registry/example/serde/json/JsonSerdeDemo.java [36:74]
public static void main(String[] args) {
String baseUrl = "http://localhost:8080";
SchemaRegistryClient schemaRegistryClient = SchemaRegistryClientFactory.newClient(baseUrl, null);
// register schema first, if have registered before ignore
String topic = "TopicTest";
RegisterSchemaRequest request = RegisterSchemaRequest.builder()
.schemaIdl("{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"},\"age\":{\"type\":\"int\"}}")
.schemaType(SchemaType.JSON)
.compatibility(Compatibility.BACKWARD)
.owner("test").build();
try {
RegisterSchemaResponse response
= schemaRegistryClient.registerSchema("default", "tanant1", topic, "Person", request);
System.out.println("register schema success, schemaId: " + response.getRecordId());
Thread.sleep(5000);
System.out.println("current schema: " + schemaRegistryClient.getSchemaBySubject(topic));
} catch (RestClientException | IOException | InterruptedException e) {
e.printStackTrace();
}
Person person = new Person(1L, "name", 18);
System.out.printf("person before serialize is %s\n", person);
try(JsonSerde<Person> jsonSerde = new JsonSerde<>(schemaRegistryClient)) {
Map<String, Object> configs = new HashMap<>();
configs.put(JsonSerdeConfig.DESERIALIZE_TARGET_TYPE, Person.class);
jsonSerde.configure(configs);
byte[] bytes = jsonSerde.serializer().serialize("TopicTest", person);
Person person1 = jsonSerde.deserializer().deserialize("TopicTest", bytes);
System.out.printf("after deserialize new person is %s\n", person1);
System.out.printf("person == person1 : %b\n", person1.equals(person));
} catch (IOException e) {
System.out.println("serde shutdown failed");
}
}