package com.lunhan.xxx.host.controller.base; import com.lunhan.xxx.common.ExecutedResult; import com.lunhan.xxx.entity.dto.NameValueDTO; import com.lunhan.xxx.host.api.NonLogin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; /** * 07.基础接口 * @order 07 */ @NonLogin @RestController @RequestMapping("base") public class BaseController { /** * 获取指定枚举类型的所有值 * @param enumName 枚举类型名称 */ @GetMapping("listEnumValue/{enumName}") public ExecutedResult> listEnumValue(@PathVariable String enumName) { try { Class enumClass = Class.forName("com.lunhan.xxx.entity.enums." + enumName); Method valuesMethod = enumClass.getMethod("values"); Method getValueMethod = enumClass.getMethod("getValue"); Method getDescMethod = enumClass.getMethod("getDesc"); Object valuesObj = valuesMethod.invoke(enumClass); Object[] values = (Object[]) valuesObj; List result = new ArrayList<>(); for(Object c : values) { String value = getValueMethod.invoke(c).toString(); String desc = getDescMethod.invoke(c).toString(); result.add(new NameValueDTO(desc, value)); } return ExecutedResult.success(result); } catch (Exception e) { return ExecutedResult.failed("获取枚举类型失败(" + enumName + "): " + e.getMessage()); } } }