
com.xiongyingqi.util.SpringMVCHelper Maven / Gradle / Ivy
/**
* YIXUN_1.5_EE
*/
package com.xiongyingqi.util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.lang.reflect.Method;
import java.util.*;
/**
* @author 瑛琪 xiongyingqi.com
* @version 2013-10-17 下午12:19:48
*/
public class SpringMVCHelper {
/**
* 获取包下所有被Controller注解的类所表示的路径
* 2013-10-17 下午1:02:45
*
* @param pkg
* @return
*/
public static Collection getAnnotationMappedPaths(Package pkg) {
if (pkg == null) {
return null;
}
Collection rs = new LinkedHashSet();
Set> classes = PackageUtil.getclass(pkg);
for (Iterator iterator = classes.iterator(); iterator.hasNext();) {
Class> clazz = (Class>) iterator.next();
if (clazz.isAnnotationPresent(Controller.class)) {
String[] clazzPaths = null;
if (clazz.isAnnotationPresent(RequestMapping.class)) {
RequestMapping typeMapping = clazz.getAnnotation(RequestMapping.class);
clazzPaths = typeMapping.value();
}
String[] methodPaths = null;
Collection methodPathCollection = new ArrayList();
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.isAnnotationPresent(RequestMapping.class)) {
RequestMapping typeMapping = method.getAnnotation(RequestMapping.class);
String[] methodPathsVar = typeMapping.value();
Collections.addAll(methodPathCollection, methodPathsVar);
}
}
if (methodPathCollection.size() > 0) {
methodPaths = methodPathCollection.toArray(new String[] {});
}
if (clazzPaths != null && clazzPaths.length > 0 && methodPaths != null
&& methodPaths.length > 0) {
for (int i = 0; i < clazzPaths.length; i++) {
String typePath = clazzPaths[i];
typePath = checkForPath(typePath);
for (int j = 0; j < methodPaths.length; j++) {
String methodPath = methodPaths[j];
methodPath = checkForPath(methodPath);
String mappedPath = typePath + methodPath;
rs.add(mappedPath);
}
}
} else if ((clazzPaths != null && clazzPaths.length > 0)
&& (methodPaths == null || methodPaths.length == 0)) {
for (int i = 0; i < clazzPaths.length; i++) {
String typePath = clazzPaths[i];
typePath = checkForPath(typePath);
rs.add(typePath);
}
} else if ((methodPaths != null && methodPaths.length > 0)
&& (clazzPaths == null || clazzPaths.length == 0)) {
EntityHelper.print(methodPaths);
for (int i = 0; i < clazzPaths.length; i++) {
String typePath = clazzPaths[i];
typePath = checkForPath(typePath);
rs.add(typePath);
}
}
}
}
return rs;
}
private static String checkForPath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy