我有一个定义如下的枚举:
public enum IntervalType {
HOUR(3600),
DAY(3600*24),
WEEK(3600*24*7),
MONTH(3600*24*30);
public Integer value;
IntervalType() {}
IntervalType(Integer value) {
this.value = value;
}
@JsonValue
public Integer toValue() {
return this.value;
}
@JsonCreator
public static IntervalType getEnumFromValue(String value) {
for (IntervalType intervalType : IntervalType.values()) {
if (intervalType.name().equals(value)) {
return intervalType;
}
}
throw new IllegalArgumentException();
}
@Override
public String toString() {
return this.name();
}
}
我的响应类定义如下:
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class IntervalType {
@JsonProperty("interval_type")
@Enumerated(EnumType.STRING)
private IntervalType intervalType;
}
我正在尝试从Spring Boot应用程序中使用Response实体返回此值,并且它给出枚举的值而不是其名称。
我该怎么做才能更改响应以使其具有名称而不是枚举的值?
答案 0 :(得分:2)
您必须添加一个以值作为参数的构造函数:
b
通常,您这样称呼它:
A
答案 1 :(得分:0)
如果您想要枚举的名称,请使用方法 name(),即:IntervalType.DAY.name()
/**
* Returns the name of this enum constant, exactly as declared in its
* enum declaration.
*
* <b>Most programmers should use the {@link #toString} method in
* preference to this one, as the toString method may return
* a more user-friendly name.</b> This method is designed primarily for
* use in specialized situations where correctness depends on getting the
* exact name, which will not vary from release to release.
*
* @return the name of this enum constant
*/
public final String name() {
return name;
}