使用JSONB(Apache johnzon)注释多态实现时发生ClassCastException

时间:2018-09-05 07:49:32

标签: java polymorphism apache-johnzon

我在JSONB(使用apache johnzon)中实现多态性时遇到一些困难。有一个限制,我不能使用GSON / JACKSON API,所以我只能使用JSONB(与apache johnzon一起使用)。

我有2个不同的json类型对象( ImportResult EmailTask​​Result ),它们属于同一父类( 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>

0 个答案:

没有答案