首页 > 解决方案 > Quarkus PanacheMongoEntity - Enum 没有自动编码/解码吗?

问题描述

这是我的实体“WebhookType”:

public class WebhookType extends PanacheMongoEntity {

    public ObjectId id;
    public String type;
    public String name;
    public String description;
    public String image;
    public List<WebhookTypeParameter> webhookTypeParameters = new ArrayList<>();
}

其中有一个“WebhookTypeParameters”列表:

public class WebhookTypeParameter {

    public Enum<ParameterType> parameterType;
    public String parameterName;
    public String parameterExample;

    public WebhookTypeParameter(){
    }

    public Enum<ParameterType> getParameterType() {
        return parameterType;
    }

    public void setParameterType(Enum<ParameterType> parameterType) {
        this.parameterType = parameterType;
    }

    public String getParameterName() {
        return parameterName;
    }

    public void setParameterName(String parameterName) {
        this.parameterName = parameterName;
    }

    public String getParameterExample() {
        return parameterExample;
    }

    public void setParameterExample(String parameterExample) {
        this.parameterExample = parameterExample;
    }

    public WebhookTypeParameter(String parameterName, Enum<ParameterType>parameterType, String parameterExample){
        this.setParameterName(parameterName);
        this.setParameterType(parameterType);
        this.setParameterExample(parameterExample);
    }
}

其中有一个 Enum 类型的字段“parameterType”:

public enum ParameterType  {
    STRING, DOUBLE;
}

现在,当尝试像这样坚持我的实体时:

        WebhookType webhookType = new WebhookType();
        webhookType.type = "XYZ";
        webhookType.name = "XYZ";
        webhookType.image = "image url";
        webhookType.description = ("Lorem Ipsum");
        webhookType.webhookTypeParameters.add(new WebhookTypeParameter("title", ParameterType.STRING, "titel xy"));
        webhookType.webhookTypeParameters.add(new WebhookTypeParameter("name", ParameterType.STRING, "foobar"));
        webhookType.persist();

我在我的日志中得到这个:

2020-09-19 14:01:07,587 ERROR [io.qua.application] (Quarkus Main Thread) Failed to start application: org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when encoding using the AutomaticPojoCodec.
Encoding a WebhookType: 'WebhookType<null>' failed with the following exception:

Failed to encode 'WebhookType'. Encoding 'webhookTypeParameters' errored with: An exception occurred when encoding using the AutomaticPojoCodec.
Encoding a WebhookTypeParameter: 'x.x.x.WebhookTypeParameter@474d3e58' failed with the following exception:

Failed to encode 'WebhookTypeParameter'. Encoding 'parameterType' errored with: Can't find a codec for class x.x.x.ParameterType.

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.

所以我的问题是我的枚举有什么问题,为什么它没有自动编码?

标签: mongodbbsonquarkus

解决方案


推荐阅读