DevelopController.java 3.9 KB

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