Jackson 将 Enum 序列化为 NAME 而不是值 (XML) Java

Jackson serializes Enum to NAME not value (XML) Java

我定义了这个枚举:

public enum OutputFormatEnum {
    PDF("pdf"),

    DOCX("docx"),

    XLSX("xlsx"),

    PPTX("pptx"),

    HTML("html"),

    PRN("prn"),

    CSV("csv"),

    RTF("rtf"),

    JPG("jpg"),

    PNG("png"),

    SVG("svg"),

    EPS("eps"),

    BMP("bmp"),

    GIF("gif"),

    TXT("txt");

    private String value;

    OutputFormatEnum(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }

    public static OutputFormatEnum fromValue(String value) {
        for (OutputFormatEnum b : OutputFormatEnum.values())
            if (b.value.equals(value))
                return b;
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
}

我有一个模型对象,它有一个名为 OutputFormat 的 OutputFormatEnum 变量。当我将此变量设置为 OutputFormatEnum.PDF 时,它最初注册为“pdf”,这是它应该的样子。当我执行以下代码时:

ByteArrayOutputStream streamTemplate = new ByteArrayOutputStream();
xmlMapper.writeValue(streamTemplate, {model_object_with_enum_variable});

设置streamTemplate中OutputFormat的值为“PDF”,这里应该是“pdf”(OutputFormatEnum.PDF的值)。知道为什么会这样吗?

您需要将@JsonValue添加到getValue()方法中,以便Jackson使用此方法序列化实例:

public enum OutputFormatEnum {
    PDF("pdf"),

    DOCX("docx"),

    XLSX("xlsx"),

    PPTX("pptx"),

    HTML("html"),

    PRN("prn"),

    CSV("csv"),

    RTF("rtf"),

    JPG("jpg"),

    PNG("png"),

    SVG("svg"),

    EPS("eps"),

    BMP("bmp"),

    GIF("gif"),

    TXT("txt");

    private String value;

    OutputFormatEnum(String value) {
        this.value = value;
    }

    @JsonValue
    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }

    public static OutputFormatEnum fromValue(String value) {
        for (OutputFormatEnum b : OutputFormatEnum.values())
            if (b.value.equals(value))
                return b;
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
}

鉴于您要将其序列化为 XML,您可能需要改用 @XmlValue

public enum OutputFormatEnum {
    PDF("pdf"),

    DOCX("docx"),

    XLSX("xlsx"),

    PPTX("pptx"),

    HTML("html"),

    PRN("prn"),

    CSV("csv"),

    RTF("rtf"),

    JPG("jpg"),

    PNG("png"),

    SVG("svg"),

    EPS("eps"),

    BMP("bmp"),

    GIF("gif"),

    TXT("txt");

    private String value;

    OutputFormatEnum(String value) {
        this.value = value;
    }

    @XmlValue
    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }

    public static OutputFormatEnum fromValue(String value) {
        for (OutputFormatEnum b : OutputFormatEnum.values())
            if (b.value.equals(value))
                return b;
        throw new IllegalArgumentException("Unexpected value '" + value + "'");
    }
}

除此之外,您还需要启用对标准 JAXB 注释的支持,如下所示:

xmlMapper.registerModule(new JaxbAnnotationModule());