SpringBoot 如何获取所有接口的 Url 地址

代码如下:

@Autowired
WebApplicationContext applicationContext;

@GetMapping("/getAllUrl")
public Object getAllUrl() {
    RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
    // 获取url与类和方法的对应信息
    Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

    List<Map<String, String>> list = new ArrayList<>();
    for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
        Map<String, String> map1 = new HashMap<>();
        RequestMappingInfo info = m.getKey();
        HandlerMethod method = m.getValue();
        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());
        }

        // 在控制台打印,方便粘贴到 Excel 中,不需要可以关掉。
        System.out.println(String.format("%s\t%s\t%s\t%s",
                map1.get("url"), map1.get("className"), map1.get("method"), map1.get("type")));

        list.add(map1);
    }
    return list;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是Spring Boot调用接口获取下载地址并下载的示例代码: ``` import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; @SpringBootApplication public class DownloadApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(DownloadApplication.class, args); } @Override public void run(String... args) throws Exception { String apiUrl = "http://example.com/api/download"; // 接口地址 RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = restTemplate.getForEntity(apiUrl, String.class); String downloadUrl = response.getBody(); // 接口返回的下载地址 // 下载文件 String fileName = "example.txt"; // 文件名 URL url = new URL(downloadUrl); BufferedInputStream inputStream = new BufferedInputStream(url.openStream()); FileOutputStream outputStream = new FileOutputStream(fileName); byte[] buffer = new byte[1024]; int count = 0; while ((count = inputStream.read(buffer, 0, 1024)) != -1) { outputStream.write(buffer, 0, count); } outputStream.close(); inputStream.close(); System.out.println("文件下载完成!"); } } ``` 在这个示例中,我们使用了Spring Boot的RestTemplate模板来调用接口获取下载地址。然后,我们使用JavaURL类打开下载地址,并使用Java的FileOutputStream将下载的文件保存到本地磁盘。最后,我们打印一条消息来确认文件下载完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不愿放下技术的小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值