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;
}

 

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在 Spring Boot 项目中,您可以使用 `ResourceLoader` 接口获取相对路径。`ResourceLoader` 是 Spring 框架中用于加载资源的接口,可以通过它来获取类路径下的文件、URL 资源等。 以下是一个示例,展示如何在 Spring Boot 项目获取相对路径: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; @Component public class ResourceExample { private final ResourceLoader resourceLoader; @Autowired public ResourceExample(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public String getRelativePath(String fileName) { try { // 使用 ResourceLoader 获取相对路径的资源 Resource resource = resourceLoader.getResource("classpath:" + fileName); return resource.getFile().getAbsolutePath(); } catch (Exception e) { // 异常处理 } return null; } } ``` 在上面的示例中,我们通过 `@Component` 注解将 `ResourceExample` 类声明为一个 Spring 组件,并通过构造函数注入了 `ResourceLoader` 对象。 然后,我们可以使用 `resourceLoader.getResource("classpath:" + fileName)` 方法获取指定文件名的资源。这里使用了 `"classpath:"` 前缀来指定资源是在类路径下查找。 最后,通过 `resource.getFile().getAbsolutePath()` 方法获取资源的绝对路径。 请注意,如果要获取的资源在类路径下的某个包中,可以在文件名中指定包路径。例如,如果要获取 `com/example/myfile.txt` 文件的相对路径,可以使用 `getRelativePath("com/example/myfile.txt")`。 希望这可以帮助到您!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值