-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de94916
commit 9319a0d
Showing
22 changed files
with
380 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/io/geekidea/springbootplus/common/controller/EnumController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright 2019-2029 geekidea(https://github.com/geekidea) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.geekidea.springbootplus.common.controller; | ||
|
||
import io.geekidea.springbootplus.common.api.ApiResult; | ||
import io.geekidea.springbootplus.common.enums.BaseEnum; | ||
import io.geekidea.springbootplus.common.vo.EnumVo; | ||
import io.geekidea.springbootplus.util.BaseEnumUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.reflections.Reflections; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.lang.reflect.Method; | ||
import java.util.*; | ||
|
||
/** | ||
* <p> | ||
* 展示实现BaseEnum接口的所有枚举值 | ||
* </p> | ||
* | ||
* @author geekidea | ||
* @date 2018/11/02 | ||
*/ | ||
@RestController | ||
@Slf4j | ||
public class EnumController { | ||
|
||
/** | ||
* 枚举包路径 | ||
*/ | ||
@Value("${spring-boot-plus.enum-packages}") | ||
private String[] enumPackages; | ||
|
||
@GetMapping("/enum") | ||
public ApiResult<String> enumList() { | ||
log.debug("enumList..."); | ||
return ApiResult.ok(BaseEnumUtil.getEnumMap()); | ||
} | ||
|
||
@PostConstruct | ||
public void init() { | ||
try { | ||
// 获取BaseEnum接口的所有实现 | ||
log.debug("enumPackages:" + Arrays.toString(enumPackages)); | ||
if (ArrayUtils.isEmpty(enumPackages)) { | ||
log.info("enumPackages为空"); | ||
return; | ||
} | ||
Reflections reflections = new Reflections(enumPackages); | ||
Set<Class<? extends BaseEnum>> set = reflections.getSubTypesOf(BaseEnum.class); | ||
if (CollectionUtils.isEmpty(set)) { | ||
return; | ||
} | ||
// 循环获取BaseEnum枚举 | ||
for (Class<? extends BaseEnum> clazz : set) { | ||
List<EnumVo> list = new ArrayList<>(); | ||
Object[] objects = clazz.getEnumConstants(); | ||
Method codeMethod = clazz.getDeclaredMethod("getCode"); | ||
Method descMethod = clazz.getDeclaredMethod("getDesc"); | ||
Map<Integer, String> codeDescMap = new HashMap<>(objects.length); | ||
for (Object object : objects) { | ||
Integer code = (Integer) codeMethod.invoke(object); | ||
String desc = (String) descMethod.invoke(object); | ||
EnumVo enumVo = new EnumVo(); | ||
enumVo.setCode(code); | ||
enumVo.setDesc(desc); | ||
list.add(enumVo); | ||
codeDescMap.put(code, desc); | ||
} | ||
// 设置map | ||
BaseEnumUtil.getEnumMap().put(clazz.getSimpleName(), list); | ||
BaseEnumUtil.getEnumClassMap().put(clazz.getName(), codeDescMap); | ||
} | ||
log.debug("baseEnumMap:{}", BaseEnumUtil.getEnumMap()); | ||
log.debug("baseEnumClassMap:{}", BaseEnumUtil.getEnumClassMap()); | ||
} catch (Exception e) { | ||
log.error("获取BaseEnum枚举map异常", e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/io/geekidea/springbootplus/common/vo/EnumVo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2019-2029 geekidea(https://github.com/geekidea) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.geekidea.springbootplus.common.vo; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* 枚举类型VO | ||
* | ||
* @author geekidea | ||
* @date 2019-11-02 | ||
**/ | ||
@Data | ||
public class EnumVo { | ||
|
||
/** | ||
* 枚举code | ||
*/ | ||
private Integer code; | ||
|
||
/** | ||
* 枚举描述 | ||
*/ | ||
private String desc; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.