Java枚举类的编写以及将枚举类转换成前端下拉列表接口的实现
首先定义一个枚举类:
package com.sample.pass.gmine.client.entity.enums;
import lombok.Getter;
import java.util.EnumSet;
/**
* 授权状态枚举
*/
@Getter
public enum CustomerLevelEnum {
/**
* 重点客户
*/
EMPHASIS(10, "重点客户"),
/**
* 一般客户
*/
COMMON(20, "一般客户");
/**
* 值
*/
private Integer value;
/**
* 名称
*/
private String name;
CustomerLevelEnum(Integer value, String name) {
this.value = value;
this.name = name;
}
/**
* 获取枚举列表
*
* @return 枚举列表
*/
public static EnumSet<CustomerLevelEnum> toEnumSet() {
return EnumSet.allOf(CustomerLevelEnum.class);
}
/**
* 根据值获取名称
*
* @param value 值
* @return 名称
*/
public static String getNameByValue(Integer value) {
if (value == null) {
return null;
}
String that = null;
for (CustomerLevelEnum e : CustomerLevelEnum.toEnumSet()) {
if (e.getValue().equals(value)) {
that = e.getName();
break;
}
}
return that;
}
/**
* 根据值获取枚举对象
*
* @param value 值
* @return 枚举对象
*/
public static CustomerLevelEnum valueOf(Integer value) {
if (value == null) {
return null;
}
CustomerLevelEnum that = null;
for (CustomerLevelEnum e : CustomerLevelEnum.toEnumSet()) {
if (e.getValue().equals(value)) {
that = e;
break;
}
}
return that;
}
@Override
public String toString() {
return String.format("{'value':%s,'name':%s}", this.value, this.name);
}
}
将枚举类转换成列表接口:
/**
* 获取客户等级
*
* @return 获取客户等级
*/
@GetMapping("/customerlevel-list")
@ApiOperation(httpMethod = "GET", value = "获取客户等级")
public R<List<JSONObject>> getCustomerlevelList() {
EnumSet<CustomerLevelEnum> customerLevelEnums = CustomerLevelEnum.toEnumSet();
List<JSONObject> result = new ArrayList<>();
customerLevelEnums.forEach(enumItem -> result.add(new JSONObject(enumItem.toString())));
return R.ok(result);
}
至此,一个枚举类和列表接口完成。
Java枚举类与前端列表接口
本文介绍如何在Java中定义枚举类,并实现将其转换为前端可用的下拉列表接口,包括枚举类的设计、枚举转列表的方法及接口实现。
1011

被折叠的 条评论
为什么被折叠?



