我该如何映射一个简单的JSON对象{"status" : "successful"}
全自动映射到我的Java枚举JAX-RS内?
public enum Status {
SUCESSFUL ("successful"),
ERROR ("error");
private String status;
private Status(String status) {
this.status = status;
}
}
如果您需要进一步的细节随意问:)
以下JAXB
注释应该这样做。 (我测试使用Jettison
,但我还没有尝试其他供应商):
@XmlType(name = "status")
@XmlEnum
public enum Status {
@XmlEnumValue(value = "successful")
SUCESSFUL,
@XmlEnumValue(value = "error")
ERROR;
}
这可以帮助你
@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;
}
}
}