You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

枚举配置多个@JsonValue报Multiple 'as-value'错误如何解决?

问题解决方法

错误根因

@JsonValue注解在单个类中仅允许标注1个方法,作用是指定Jackson序列化该类实例时输出的唯一值。你同时在getShortName()和getFullName()两个方法上添加了@JsonValue,Jackson无法判断该用哪个字段作为枚举序列化的最终结果,因此抛出冲突错误。

修复方案

方案1:保留单值序列化逻辑

如果常规接口交互仅需要用shortName作为状态传输值,直接删除getFullName()方法上的@JsonValue注解即可,原有反序列化逻辑不需要修改。
如果需要同时返回shortName和fullName给前端生成下拉选项,可新增一个静态方法输出全部选项列表,不需要依赖@JsonValue实现:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum BusinessCustomersStatus {
    A("active", "Active"),
    O("onboarding", "Onboarding"),
    NV("not_verified", "Not Verified"),
    V("verified", "Verified"),
    S("suspended", "Suspended"),
    I("inactive", "Inactive");

    private String shortName;
    private String fullName;

    BusinessCustomersStatus(String shortName, String fullName) {
        this.shortName = shortName;
        this.fullName = fullName;
    }

    @JsonValue
    public String getShortName() {
        return shortName;
    }

    // 移除该方法上的@JsonValue注解
    public String getFullName() {
        return fullName;
    }

    @JsonCreator
    public static BusinessCustomersStatus fromStatus(String statusText) {
        for (BusinessCustomersStatus status : values()) {
            if (status.getShortName().equalsIgnoreCase(statusText)) {
                return status;
            }
        }
        throw new UnsupportedOperationException(String.format("Unknown status: '%s'", statusText));
    }

    // 新增方法返回所有选项,用于前端下拉框渲染
    public static List<Map<String, String>> getAllOptions() {
        List<Map<String, String>> options = new ArrayList<>();
        for (BusinessCustomersStatus status : values()) {
            Map<String, String> item = new HashMap<>();
            item.put("value", status.shortName);
            item.put("label", status.fullName);
            options.add(item);
        }
        return options;
    }
}

方案2:多场景序列化适配

如果需要在不同接口返回不同的枚举字段,可使用Jackson的@JsonView做视图区分:

// 定义视图类
public class Views {
    public interface ShortStatus {}
    public interface FullStatus extends ShortStatus {}
}

// 枚举修改注解
@JsonValue
@JsonView(Views.ShortStatus.class)
public String getShortName() {
    return shortName;
}

@JsonView(Views.FullStatus.class)
public String getFullName() {
    return fullName;
}

在对应Controller接口上添加@JsonView(Views.ShortStatus.class)或@JsonView(Views.FullStatus.class)即可控制返回的字段。

额外注意事项

你当前请求payload中传的status值为assigned,不在你枚举定义的shortName列表中,修复序列化问题后,请求时需要传active/onboarding/not_verified等枚举中存在的shortName值,否则会触发反序列化异常。

内容的提问来源于stack exchange,提问作者Peter Penzov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 00:15:02