DevelopController.java 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.utils.ObjUtils;
  3. import org.reflections.Reflections;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.*;
  6. import org.springframework.web.method.HandlerMethod;
  7. import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition;
  8. import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
  9. import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
  10. import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
  11. import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
  12. import org.springframework.web.util.pattern.PathPattern;
  13. import javax.persistence.Entity;
  14. import java.util.*;
  15. @RestController
  16. @RequestMapping("/dev")
  17. // @PreAuthorize("hasRole('ROLE_ADMIN') and hasRole('ROLE_DEV')")
  18. public class DevelopController {
  19. @Autowired
  20. private RequestMappingHandlerMapping requestMappingHandlerMapping;
  21. @GetMapping("/entities")
  22. public List entities() {
  23. List<Map<String, String>> entities = new ArrayList<>();
  24. Reflections reflections = new Reflections(this.getClass().getPackage().getName().replace(".web", ".domain"));
  25. Set<Class<?>> entitySet = reflections.getTypesAnnotatedWith(Entity.class);
  26. for (Class<?> entity : entitySet) {
  27. Map<String, String> map = new HashMap<>();
  28. map.put("name", entity.getSimpleName());
  29. map.put("package", entity.getName());
  30. entities.add(map);
  31. }
  32. return entities;
  33. }
  34. @GetMapping("/getFields")
  35. public String[] getFields(@RequestParam String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
  36. return ObjUtils.getFields(Class.forName(className).newInstance().getClass());
  37. }
  38. @GetMapping("/mappings")
  39. @ResponseBody
  40. public List list() {
  41. List<HashMap<String, String>> urlList = new ArrayList<>();
  42. Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
  43. for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
  44. HashMap<String, String> hashMap = new HashMap<>();
  45. RequestMappingInfo info = m.getKey();
  46. HandlerMethod method = m.getValue();
  47. PathPatternsRequestCondition p = info.getPathPatternsCondition();
  48. if (p == null) {
  49. continue;
  50. }
  51. for (PathPattern url : p.getPatterns()) {
  52. hashMap.put("url", url.getPatternString());
  53. }
  54. hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
  55. hashMap.put("method", method.getMethod().getName()); // 方法名
  56. RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
  57. String type = methodsCondition.toString();
  58. if (type != null && type.startsWith("[") && type.endsWith("]")) {
  59. type = type.substring(1, type.length() - 1);
  60. hashMap.put("type", type); // 方法名
  61. }
  62. urlList.add(hashMap);
  63. }
  64. return urlList;
  65. }
  66. @GetMapping("/selectMappings")
  67. @ResponseBody
  68. public List selectMappings() {
  69. List<HashMap<String, String>> urlList = new ArrayList<>();
  70. Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
  71. for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
  72. HashMap<String, String> hashMap = new HashMap<>();
  73. RequestMappingInfo info = m.getKey();
  74. HandlerMethod method = m.getValue();
  75. PatternsRequestCondition p = info.getPatternsCondition();
  76. for (String url : p.getPatterns()) {
  77. hashMap.put("url", url);
  78. }
  79. urlList.add(hashMap);
  80. }
  81. return urlList;
  82. }
  83. }