Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我们使用Enum-Jackson JSON时,找不到适合类型的构造函数_Java_Json_Enums_Jackson - Fatal编程技术网

Java 当我们使用Enum-Jackson JSON时,找不到适合类型的构造函数

Java 当我们使用Enum-Jackson JSON时,找不到适合类型的构造函数,java,json,enums,jackson,Java,Json,Enums,Jackson,我有一门课看起来像这样: public class Content { public enum Type { TEXT, URL, FILE } public enum Rendering { MARKDOWN, HTML, PLAIN, AUTO } public final Type type; public final Renderin

我有一门课看起来像这样:

   public class Content {

        public enum Type {
            TEXT, URL, FILE
        }

        public enum Rendering {
            MARKDOWN, HTML, PLAIN, AUTO
        }

        public final Type type;
        public final Rendering rendering;
        public final String content;

        public Content(Type type, Rendering rendering, String content) {
            this.type = type;
            this.rendering = (rendering != null ? rendering : Rendering.AUTO);
            this.content = content;
        }

    }
{
    "type": "TEXT",
    "rendering": "AUTO",
    "content": "Lorem ipsum"
}
    public static class Image {
        public enum Display {
            COVER, CONTAIN, START, END
        }
        public enum Type {
            BASE64, FILE, URL
        }
        public final Type type; // <-- this!
        public final Display display;
        public final int padding;
        public final String content;

        public Image(Type type, Display display, int padding, String content) {
            this.type = type;
            this.display = display;
            this.padding = padding;
            this.content = content;
        }
    }
我得到了一个JSON字符串,如下所示:

   public class Content {

        public enum Type {
            TEXT, URL, FILE
        }

        public enum Rendering {
            MARKDOWN, HTML, PLAIN, AUTO
        }

        public final Type type;
        public final Rendering rendering;
        public final String content;

        public Content(Type type, Rendering rendering, String content) {
            this.type = type;
            this.rendering = (rendering != null ? rendering : Rendering.AUTO);
            this.content = content;
        }

    }
{
    "type": "TEXT",
    "rendering": "AUTO",
    "content": "Lorem ipsum"
}
    public static class Image {
        public enum Display {
            COVER, CONTAIN, START, END
        }
        public enum Type {
            BASE64, FILE, URL
        }
        public final Type type; // <-- this!
        public final Display display;
        public final int padding;
        public final String content;

        public Image(Type type, Display display, int padding, String content) {
            this.type = type;
            this.display = display;
            this.padding = padding;
            this.content = content;
        }
    }
现在,由于
Content
类中的字段是最终字段,Jackson将无法工作,因此我使用:

注意:我本可以使用构造函数参数对原始类进行注释,但是
内容
来自我无法修改的库

我就是这样使用它们的:

    // ... 
    SimpleModule module = new SimpleModule();
    module.setMixInAnnotation(Content.class, ContentMixIn.class);
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(module);
    mapper.readValue("<the json>", Content.class);
为什么??通过简单地查看行号,我假设它在抱怨
枚举类型
,但是我该如何纠正这一点呢

PS:MixIn设置正确,因为当我用字符串值替换我的内容构造函数的枚举参数时,它可以工作(尽管不是我想要的方式):


基本上,我导入了错误的类型,因为我使用的是Intellij,当它询问要导入哪种类型时,我没有注意

与Jackson合作时,注意类型非常重要。

特别感谢@Sotirios,他给了我关于这个问题的各种建议,这在调试方面对我帮助很大。 结果我得到了另一个类似这样的类:

   public class Content {

        public enum Type {
            TEXT, URL, FILE
        }

        public enum Rendering {
            MARKDOWN, HTML, PLAIN, AUTO
        }

        public final Type type;
        public final Rendering rendering;
        public final String content;

        public Content(Type type, Rendering rendering, String content) {
            this.type = type;
            this.rendering = (rendering != null ? rendering : Rendering.AUTO);
            this.content = content;
        }

    }
{
    "type": "TEXT",
    "rendering": "AUTO",
    "content": "Lorem ipsum"
}
    public static class Image {
        public enum Display {
            COVER, CONTAIN, START, END
        }
        public enum Type {
            BASE64, FILE, URL
        }
        public final Type type; // <-- this!
        public final Display display;
        public final int padding;
        public final String content;

        public Image(Type type, Display display, int padding, String content) {
            this.type = type;
            this.display = display;
            this.padding = padding;
            this.content = content;
        }
    }
公共静态类映像{
公共枚举显示{
覆盖、包含、开始、结束
}
公共枚举类型{
BASE64、文件、URL
}

public final Type;//您使用的是什么版本的Jackson?您的示例在2.3.0中运行良好。您也不需要
@JsonCreator
。我切换到了2.3.0,结果完全相同,有什么建议吗?您确定该类与您在这里介绍的一样吗?在PS中,您谈到了一个具有三个
St的构造函数ring
参数。这可能是
Content
类中唯一的构造函数吗?