原创

Annotation p = method.getAnnotation(RequestMapping.class)始终为null


import java.io.IOException;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * w t f ??? spring-mvc 3.1 
 * TODO RequestMapping 
 */
//import org.springframework.web.method.HandlerMethod;
import org.apache.http.NameValuePair;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.tags.Param;


/**
 * 
 * 
 * @author Link
 * 
 */
public class ShverCon extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // System.out.println("");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        if (doShver(request, response, handler)) {
            return true;
        } else {
            out.write("{\"code\":\"-1\",\"data\":\"no login or key error\"}");
            out.close();
            return false;
        }
    }
    /**
     * 
     * 
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws SecurityException
     * @throws NoSuchMethodException
     */
    private boolean doShver(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws IllegalAccessExceptionIllegalArgumentException, InvocationTargetException, NoSuchMethodException,
            SecurityException {
        String className = handler.getClass().getName();
        // className =
        // className.substring(0,className.indexOf("$"));//pakageName.className$xxx$xx
        Class<?> clazz = null;
        try {
            clazz = Class.forName(className);
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        Method[] methods = clazz.getDeclaredMethods();
        String uri = request.getRequestURI(); // URI/user/getuser
        // url
        String finalURI = uri.substring(uri.lastIndexOf("/")).replace("/""");
        if(finalURI.equals("")){
            finalURI=uri.substring(uri.indexOf("botfetcher/")).replace("/""").replace("botfetcher""");
            
        }
        // 
        RoleShver.UserType needRole = null;
        // ID
        String shveridParamName = null;
        
        // 
        String key = null;
        for (Method method : methods) {
            //if (method.isAnnotationPresent((Class<? extends Annotation>)RequestMapping.class)) {
                Annotation p = method.getAnnotation(RequestMapping.class);
                if(p!=null){
                Method m = p.getClass().getDeclaredMethod("value"null);
                String[] strs = (String[]) m.invoke(p, null);
                String strs0 = strs[0].replace("/""");
                // System.out.println("strs0="+strs0+" and "+"finalURI="+finalURI);
                // urlUrl()
                if (strs0.equals(finalURI)) {
                    RoleShver roleShver = method.getAnnotation(RoleShver.class);
                    // 
                    
                     key = request.getParameter("key");
                     if(key==null||!key.equals(Constants.KEY))
                     {
                         return false;
                     }
                    if (roleShver == null) {
                        // 
                        
                        return true;
                    }else {
                        // 
                        needRole = roleShver.value();
                        shveridParamName = roleShver.shverid();
                        // System.out.println(shveridParamName);
                        break;
                    }
                } else {
                    continue;
                }}
            //}
        }
    
        
        // shverid
        String shverid = getShverId(request, shveridParamName);
        // 
        if (shverid == null || shverid.length() == 0) {
            return false;
        }
        //bothshveridid-1
        if(needRole.equals(RoleShver.UserType.BOTH)){
            Integer uid=ShverCache.getUserId(shverid);
            if(uid==-1){
                return false;
            }
        }
        
        ShverCache.updateLastTime(shverid);
        System.out.println("" + shverid);
        System.out.println("" + ShverCache.getUserType(shverid));
        System.out.println("" + needRole);
        return ShverCache.getUserType(shverid).equals(needRole) || needRole.equals(RoleShver.UserType.NOLOGIN) || needRole.equals(RoleShver.UserType.BOTH);
    }
    private String getShverId(HttpServletRequest request, String shveridName) {
        // shverid
        List<org.apache.http.NameValuePair> params = new ArrayList<NameValuePair>();
        String shverid = null;
        Map<StringString> parameters = new HashMap<StringString>();
        for (final String key : request.getParameterMap().keySet()) {
            if (key.compareTo(shveridName) == 0) {
                shverid = String.valueOf(((String[]) request.getParameterMap().get(key))[0]);
            }
        }
        return shverid;
    }
}
 


 上面的代码Annotation p = method.getAnnotation(RequestMapping.class)这句始终是获取为null

解决方法:
 <context:annotation-config />
<context:component-scan base-package="com.skf.mokao.controller" />
<aop:aspectj-autoproxy proxy-target- />
<mvc:annotation-driven />

在spring的配置文件中删除下面的配置信息即可
<aop:aspectj-autoproxy proxy-target- />

正文到此结束
本文目录