ClassCastException при использовании JSONB (apache johnzon) Аннотация Полиморфная реализация
Я сталкиваюсь с некоторыми трудностями в реализации полиморфизма в JSONB (с Apache Johnzon). Есть ограничение, что я не могу использовать GSON/JACKSON API, поэтому я должен использовать только JSONB (с apache johnzon).
У меня есть 2 разных объекта типа JSON (ImportResult & EmailTaskResult), которые принадлежат к одному родительскому классу (JobResult). Я сталкиваюсь с некоторыми трудностями в его реализации аннотации.
Я следую этому уроку: https://johnzon.apache.org/
// =========================== MAIN CLASS ====================================================
import javax.json.bind.JsonbBuilder;
public class PolyExample {
public static void main(String[] args) {
String ir = "{\"@type\": \"ImportResult\", \"created\": \"abcabc\"}";
String er = "{\"@type\": \"EmailTaskResult\", \"firstFailedWelcomeEmail\": null}";
JobResult res = JsonbBuilder.create().fromJson(er, new JobTask().getClass().getGenericSuperclass());
System.out.println(res);
}
}
Exception in thread "main" java.lang.ClassCastException: java.util.HashMap cannot be cast to com.cia.api.model.JobResult
at com.cia.api.model.PolyExample.main(PolyExample.java:18)
// ============= INTERFACE ==========================
import org.apache.johnzon.jsonb.extras.polymorphism.Polymorphic;
@Polymorphic.JsonChildren({
ImportResult.class,
EmailTaskResult.class
})
public interface JobResult {
}
// ================================================
import org.apache.johnzon.jsonb.extras.polymorphism.Polymorphic;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
@Polymorphic.JsonId("ImportResult")
public class ImportResult implements JobResult {
// Member(s)
private String created;
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
}
// ================================================
import org.apache.johnzon.jsonb.extras.polymorphism.Polymorphic;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
@Polymorphic.JsonId("EmailTaskResult")
public class EmailTaskResult implements JobResult {
// Member(s)
private String firstFailedWelcomeEmail;
public String getFirstFailedWelcomeEmail() {
return firstFailedWelcomeEmail;
}
public void setFirstFailedWelcomeEmail(String firstFailedWelcomeEmail) {
this.firstFailedWelcomeEmail = firstFailedWelcomeEmail;
}
}
// ============================================================================================
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.annotation.JsonbProperty;
import javax.json.bind.annotation.JsonbTypeDeserializer;
import javax.json.bind.annotation.JsonbTypeSerializer;
import org.apache.johnzon.jsonb.extras.polymorphism.Polymorphic;
public class JobTask {
// Member(s)
@JsonbProperty("result")
@JsonbTypeSerializer(Polymorphic.Serializer.class)
@JsonbTypeDeserializer(Polymorphic.DeSerializer.class)
private JobResult result;
public JobResult getResult() {
return result;
}
public void setResult(JobResult result) {
this.result = result;
}
}
// =========================== POM File ====================================================
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-json_1.1_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.johnzon</groupId>
<artifactId>johnzon-jsonb</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.johnzon</groupId>
<artifactId>johnzon-core</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.johnzon</groupId>
<artifactId>johnzon-mapper</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.johnzon</groupId>
<artifactId>johnzon-jsonb-extras</artifactId>
<version>1.1.8</version>
</dependency>