欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot项目如何获取所有的接口

程序员文章站 2022-03-14 08:11:00
目录springboot项目获取所有接口springboot项目获取所有接口@autowiredprivate webapplicationcontext applicationcontext; @o...

springboot项目获取所有接口

@autowired
private webapplicationcontext applicationcontext; 
@override
public list getallurl() {
    requestmappinghandlermapping mapping = applicationcontext.getbean(requestmappinghandlermapping.class);
    // 获取url与类和方法的对应信息
    map<requestmappinginfo, handlermethod> map = mapping.gethandlermethods();
 
    list<map<string, string>> list = new arraylist<map<string, string>>();
    for (map.entry<requestmappinginfo, handlermethod> m : map.entryset()) {
        map<string, string> map1 = new hashmap<string, string>();
        requestmappinginfo info = m.getkey();
        handlermethod method = m.getvalue();
        //获取当前方法所在类名
        class<?> bean = method.getbeantype();
        //使用反射获取当前类注解内容
        api api = bean.getannotation(api.class);
        requestmapping requestmapping = bean.getannotation(requestmapping.class);
  string[] value = requestmapping.value();
  map1.put("parent",value[0])
        //获取方法上注解以及注解值
        apioperation methodannotation = method.getmethodannotation(apioperation.class);
        string privilegename = methodannotation.notes();
        patternsrequestcondition p = info.getpatternscondition();
        for (string url : p.getpatterns()) {
            map1.put("url", url);
        }
        map1.put("classname", method.getmethod().getdeclaringclass().getname()); // 类名
        map1.put("method", method.getmethod().getname()); // 方法名
        requestmethodsrequestcondition methodscondition = info.getmethodscondition();
        for (requestmethod requestmethod : methodscondition.getmethods()) {
            map1.put("type", requestmethod.tostring());
        }
        
        list.add(map1);
    } 
   return list;
}

获取项目下所有http接口的信息

一、接口信息类

新建一个类用于存放http接口的相关信息

class requesttomethoditem {
	public string controllername;
    public string methodname;
    public string requesttype;
    public string requesturl;
    public class<?>[] methodparmatypes;    
    public string getcontrollername() {
		return controllername;
	}
	public void setcontrollername(string controllername) {
		this.controllername = controllername;
	}
	public string getmethodname() {
		return methodname;
	}
	public void setmethodname(string methodname) {
		this.methodname = methodname;
	}
	public string getrequesttype() {
		return requesttype;
	}
	public void setrequesttype(string requesttype) {
		this.requesttype = requesttype;
	}
	public string getrequesturl() {
		return requesturl;
	}
	public void setrequesturl(string requesturl) {
		this.requesturl = requesturl;
	}
	public class<?>[] getmethodparmatypes() {
		return methodparmatypes;
	}
	public void setmethodparmatypes(class<?>[] methodparmatypes) {
		this.methodparmatypes = methodparmatypes;
	}
    public requesttomethoditem(string requesturl, string requesttype, string controllername, string requestmethodname, class<?>[] methodparmatypes){
        this.requesturl = requesturl;
        this.requesttype = requesttype;
        this.controllername = controllername;
        this.methodname = requestmethodname;
        this.methodparmatypes = methodparmatypes;
    }
}

二、单元测试

写两个http接口用于测试

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.responsebody;
@controller
public class testcontroller {
	@getmapping(value = "/test1")
	@responsebody
	public void test1(integer a) {
	}
	
	@postmapping(value = "/test2")
	@responsebody
	public void test2(integer a,integer b) {
	}
}

测试单元

import java.util.arraylist;
import java.util.list;
import java.util.map;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
import org.springframework.web.context.webapplicationcontext;
import org.springframework.web.method.handlermethod;
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 cn.hutool.json.jsonutil; //hutool是一个很方便的工具包
@springboottest
@runwith(springrunner.class)
public class test {	
    @autowired
    webapplicationcontext applicationcontext;
    
	@org.junit.test
	public void index() {
		list<requesttomethoditem> requesttomethoditemlist = new arraylist<requesttomethoditem>();
        requestmappinghandlermapping requestmappinghandlermapping = applicationcontext.getbean(requestmappinghandlermapping.class);
        map<requestmappinginfo, handlermethod> handlermethods = requestmappinghandlermapping.gethandlermethods();
		for (map.entry<requestmappinginfo, handlermethod> requestmappinginfohandlermethodentry : handlermethods
				.entryset()) {
			requestmappinginfo requestmappinginfo = requestmappinginfohandlermethodentry.getkey();				
			requestmethodsrequestcondition methodcondition = requestmappinginfo.getmethodscondition();
			patternsrequestcondition patternscondition = requestmappinginfo.getpatternscondition();
			handlermethod mappinginfovalue = requestmappinginfohandlermethodentry.getvalue();
			
			// 请求类型
			string requesttype = methodcondition.getmethods().tostring();			
			// 请求路径
			string requesturl = patternscondition.getpatterns().iterator().next();
			// 控制器名称
			string controllername = mappinginfovalue.getbeantype().tostring();
			// 请求方法名
			string requestmethodname = mappinginfovalue.getmethod().getname();
			// 请求参数
			class<?>[] methodparamtypes = mappinginfovalue.getmethod().getparametertypes();
			
			// spring通过basicerrorcontroller进行统一的异常处理,不计入这些api
			if("class org.springframework.boot.autoconfigure.web.servlet.error.basicerrorcontroller".equals(controllername)) {
				continue;
			}
			
			requesttomethoditem item = new requesttomethoditem(requesturl, requesttype, controllername,
					requestmethodname, methodparamtypes);
			requesttomethoditemlist.add(item);						
		}
		
		system.out.println(jsonutil.tojsonstr(requesttomethoditemlist));		
	}
}

测试结果

Springboot项目如何获取所有的接口

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。