|
| 1 | +/* |
| 2 | + * Copyright 2019-2029 geekidea(https://github.com/geekidea) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.geekidea.springbootplus.common.controller; |
| 18 | + |
| 19 | +import io.geekidea.springbootplus.common.api.ApiResult; |
| 20 | +import io.geekidea.springbootplus.common.enums.BaseEnum; |
| 21 | +import io.geekidea.springbootplus.common.vo.EnumVo; |
| 22 | +import io.geekidea.springbootplus.util.BaseEnumUtil; |
| 23 | +import lombok.extern.slf4j.Slf4j; |
| 24 | +import org.apache.commons.collections4.CollectionUtils; |
| 25 | +import org.apache.commons.lang3.ArrayUtils; |
| 26 | +import org.reflections.Reflections; |
| 27 | +import org.springframework.beans.factory.annotation.Value; |
| 28 | +import org.springframework.web.bind.annotation.GetMapping; |
| 29 | +import org.springframework.web.bind.annotation.RestController; |
| 30 | + |
| 31 | +import javax.annotation.PostConstruct; |
| 32 | +import java.lang.reflect.Method; |
| 33 | +import java.util.*; |
| 34 | + |
| 35 | +/** |
| 36 | + * <p> |
| 37 | + * 展示实现BaseEnum接口的所有枚举值 |
| 38 | + * </p> |
| 39 | + * |
| 40 | + * @author geekidea |
| 41 | + * @date 2018/11/02 |
| 42 | + */ |
| 43 | +@RestController |
| 44 | +@Slf4j |
| 45 | +public class EnumController { |
| 46 | + |
| 47 | + /** |
| 48 | + * 枚举包路径 |
| 49 | + */ |
| 50 | + @Value("${spring-boot-plus.enum-packages}") |
| 51 | + private String[] enumPackages; |
| 52 | + |
| 53 | + @GetMapping("/enum") |
| 54 | + public ApiResult<String> enumList() { |
| 55 | + log.debug("enumList..."); |
| 56 | + return ApiResult.ok(BaseEnumUtil.getEnumMap()); |
| 57 | + } |
| 58 | + |
| 59 | + @PostConstruct |
| 60 | + public void init() { |
| 61 | + try { |
| 62 | + // 获取BaseEnum接口的所有实现 |
| 63 | + log.debug("enumPackages:" + Arrays.toString(enumPackages)); |
| 64 | + if (ArrayUtils.isEmpty(enumPackages)) { |
| 65 | + log.info("enumPackages为空"); |
| 66 | + return; |
| 67 | + } |
| 68 | + Reflections reflections = new Reflections(enumPackages); |
| 69 | + Set<Class<? extends BaseEnum>> set = reflections.getSubTypesOf(BaseEnum.class); |
| 70 | + if (CollectionUtils.isEmpty(set)) { |
| 71 | + return; |
| 72 | + } |
| 73 | + // 循环获取BaseEnum枚举 |
| 74 | + for (Class<? extends BaseEnum> clazz : set) { |
| 75 | + List<EnumVo> list = new ArrayList<>(); |
| 76 | + Object[] objects = clazz.getEnumConstants(); |
| 77 | + Method codeMethod = clazz.getDeclaredMethod("getCode"); |
| 78 | + Method descMethod = clazz.getDeclaredMethod("getDesc"); |
| 79 | + Map<Integer, String> codeDescMap = new HashMap<>(objects.length); |
| 80 | + for (Object object : objects) { |
| 81 | + Integer code = (Integer) codeMethod.invoke(object); |
| 82 | + String desc = (String) descMethod.invoke(object); |
| 83 | + EnumVo enumVo = new EnumVo(); |
| 84 | + enumVo.setCode(code); |
| 85 | + enumVo.setDesc(desc); |
| 86 | + list.add(enumVo); |
| 87 | + codeDescMap.put(code, desc); |
| 88 | + } |
| 89 | + // 设置map |
| 90 | + BaseEnumUtil.getEnumMap().put(clazz.getSimpleName(), list); |
| 91 | + BaseEnumUtil.getEnumClassMap().put(clazz.getName(), codeDescMap); |
| 92 | + } |
| 93 | + log.debug("baseEnumMap:{}", BaseEnumUtil.getEnumMap()); |
| 94 | + log.debug("baseEnumClassMap:{}", BaseEnumUtil.getEnumClassMap()); |
| 95 | + } catch (Exception e) { |
| 96 | + log.error("获取BaseEnum枚举map异常", e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments