package com.izouma.nineth.web; import com.izouma.nineth.utils.ObjUtils; import org.reflections.Reflections; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition; import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.util.pattern.PathPattern; import javax.persistence.Entity; import java.util.*; @RestController @RequestMapping("/dev") // @PreAuthorize("hasRole('ROLE_ADMIN') and hasRole('ROLE_DEV')") public class DevelopController { @Autowired private RequestMappingHandlerMapping requestMappingHandlerMapping; @GetMapping("/entities") public List entities() { List> entities = new ArrayList<>(); Reflections reflections = new Reflections(this.getClass().getPackage().getName().replace(".web", ".domain")); Set> entitySet = reflections.getTypesAnnotatedWith(Entity.class); for (Class entity : entitySet) { Map map = new HashMap<>(); map.put("name", entity.getSimpleName()); map.put("package", entity.getName()); entities.add(map); } return entities; } @GetMapping("/getFields") public String[] getFields(@RequestParam String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException { return ObjUtils.getFields(Class.forName(className).newInstance().getClass()); } @GetMapping("/mappings") @ResponseBody public List list() { List> urlList = new ArrayList<>(); Map map = requestMappingHandlerMapping.getHandlerMethods(); for (Map.Entry m : map.entrySet()) { HashMap hashMap = new HashMap<>(); RequestMappingInfo info = m.getKey(); HandlerMethod method = m.getValue(); PathPatternsRequestCondition p = info.getPathPatternsCondition(); if (p == null) { continue; } for (PathPattern url : p.getPatterns()) { hashMap.put("url", url.getPatternString()); } hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名 hashMap.put("method", method.getMethod().getName()); // 方法名 RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition(); String type = methodsCondition.toString(); if (type != null && type.startsWith("[") && type.endsWith("]")) { type = type.substring(1, type.length() - 1); hashMap.put("type", type); // 方法名 } urlList.add(hashMap); } return urlList; } @GetMapping("/selectMappings") @ResponseBody public List selectMappings() { List> urlList = new ArrayList<>(); Map map = requestMappingHandlerMapping.getHandlerMethods(); for (Map.Entry m : map.entrySet()) { HashMap hashMap = new HashMap<>(); RequestMappingInfo info = m.getKey(); HandlerMethod method = m.getValue(); PatternsRequestCondition p = info.getPatternsCondition(); for (String url : p.getPatterns()) { hashMap.put("url", url); } urlList.add(hashMap); } return urlList; } }