如何手动映射在JAX-RS枚举领域(How to manually map Enum fields

2019-07-31 08:16发布

我该如何映射一个简单的JSON对象{"status" : "successful"}全自动映射到我的Java枚举JAX-RS内?

public enum Status {
    SUCESSFUL ("successful"), 
    ERROR ("error");

    private String status;

    private Status(String status) {
        this.status = status;
    }
}

如果您需要进一步的细节随意问:)

Answer 1:

以下JAXB注释应该这样做。 (我测试使用Jettison ,但我还没有尝试其他供应商):

@XmlType(name = "status")
@XmlEnum
public enum Status {
    @XmlEnumValue(value = "successful")
    SUCESSFUL, 
    @XmlEnumValue(value = "error")
    ERROR;
}


Answer 2:

这可以帮助你

@Entity
public class Process {

  private State state;

  public enum State {
    RUNNING("running"), STOPPED("stopped"), PAUSED("paused");

    private String value;

    private State(String value) {
      this.value = value;
    }

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

    @JsonCreator
    public static State create(String val) {
      State[] states = State.values();
      for (State state : states) {
        if (state.getValue().equalsIgnoreCase(val)) {
          return state;
        }
      }
      return STOPPED;
    }
  }
}


文章来源: How to manually map Enum fields in JAX-RS