码迷,mamicode.com
首页 > 编程语言 > 详细

打印 SpringMVC中所有的接口URL

时间:2016-04-20 13:14:21      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:

采用junit test方式

1.配置  simple-test.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="requestMappingHandlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
    <bean id="requestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
</beans>

 

 

2.Junit Test Case

package com.xxx.msa.test;

import java.util.Map;
import java.util.Map.Entry;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

public class TestUrl {
	
    @Autowired
    private static RequestMappingHandlerAdapter handlerAdapter;

    @Autowired
    private static RequestMappingHandlerMapping handlerMapping;
	
    	@BeforeClass
	    public static void before(){
	    	ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{
	    			"classpath*:config/spring/spring-main.xml", 
	    			"classpath*:config/spring/springmvc/springMVC.xml"
	    			,"classpath*:config/spring/simple-test.xml"});
	    	handlerAdapter=(RequestMappingHandlerAdapter) ctx.getBean("requestMappingHandlerAdapter");
	    	handlerMapping=(RequestMappingHandlerMapping) ctx.getBean("requestMappingHandlerMapping");
	    }
	 
      
	
	@Test
	public void testUrls(){
		Map<RequestMappingInfo, HandlerMethod> handlerMethods =handlerMapping
				.getHandlerMethods();

		for (Entry<RequestMappingInfo, HandlerMethod> item : handlerMethods
				.entrySet()) {
			RequestMappingInfo mapping = item.getKey();
			HandlerMethod method = item.getValue();

			for (String urlPattern : mapping.getPatternsCondition()
					.getPatterns()) {
				System.out.println(method.getBeanType().getName() + "#"
						+ method.getMethod().getName() + " <-- " + urlPattern);

				if (urlPattern.equals("some specific url")) {
					// add to list of matching METHODS
				}
			}
		}    
		
	}

}

 

 

 

3.结果

技术分享

打印 SpringMVC中所有的接口URL

标签:

原文地址:http://www.cnblogs.com/qq-757617012/p/5411921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!