黑科技-SpringMVC打印所有的RequstMapping信息

    这个代码大部分是我抄的别的地方的,我试了下不打印就自己改了下。不多说了,下面是代码。

   改动的地方有:

        用json返回;

        WebApplicationContext appContext注入;

    原代码网址就不发了。下面代码是可以正常输出的。

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerMapping;
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 com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/seminar")
public class ShowAllMappingInfoController {

	@Autowired
	WebApplicationContext appContext;
	
	@RequestMapping(value = "/showAllMappingInfo", method = RequestMethod.GET)
	@ResponseBody
    public JSONArray index(HttpServletRequest request)
    {

        //请求url和处理方法的映射
        JSONArray requestToMethodItemJSONArray = new JSONArray();
        
        //获取所有的RequestMapping
        Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, 
HandlerMapping.class, true, false);

        for (HandlerMapping handlerMapping : allRequestMappings.values())
        {
            //本项目只需要RequestMappingHandlerMapping中的URL映射
            if (handlerMapping instanceof RequestMappingHandlerMapping)
            {
                RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;
                Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
                for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet())
                {
                    RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();
                    HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue();

                    RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition();

                    PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
                    String requestUrl = patternsCondition.getPatterns().toString();

                    String controllerName = mappingInfoValue.getBeanType().toString();
                    String requestMethodName = mappingInfoValue.getMethod().getName();
                    Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();
                    
                    
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("requestUrl", requestUrl);
                    jsonObject.put("controllerName", controllerName);
                    jsonObject.put("requestMethodName", requestMethodName);
                    
                    requestToMethodItemJSONArray.add(jsonObject);
                }
                break;
            }
        }
        return requestToMethodItemJSONArray;
    }
}

猜你喜欢

转载自my.oschina.net/u/1444945/blog/1068914