枚举类型前后端交互取枚举值,枚举把值放在List中

    xiaoxiao2022-07-07  198

    枚举类在后端中的编写

    public enum TaskTriggerTypeEnum { CHILD((byte) 1, "儿童"), YOUTH((byte) 2, "青年"); Byte type; String desc; TaskTriggerTypeEnum(Byte type, String desc) { this.type = type; this.desc = desc; } public Byte getType() { return type; } public void setType(Byte type) { this.type = type; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public static boolean contain(Byte type) { if (null == type) { return false; } for (TaskTriggerTypeEnum taskTriggerTypeEnum : values()) { if (type == taskTriggerTypeEnum.getType()) { return true; } } return false; } public static TaskTriggerTypeEnum getEnumByType(Byte triggerType) { if (triggerType != null) { for (TaskTriggerTypeEnum flowTriggerType : TaskTriggerTypeEnum.values()) { if (flowTriggerType.getType().equals(triggerType)) { return flowTriggerType; } } } return null; } }

    在后端中获取所有枚举类型中的值

    @GetMapping(value = "/getTypeEnum") //返回list型数据,存储枚举中的值 public List getAllTypeEnum() { List allValue = new ArrayList<>(); for(TaskTriggerTypeEnum s :TaskTriggerTypeEnum.values()){ Map<Byte,String> map = new HashMap<>(); map.put(s.getType(),s.getDesc()); allValue.add(map); } return allValue; }

     

    最新回复(0)