springboot通过方法导出所有接口URI

springboot通过方法导出所有接口URI

一.导出类名、方法名、URL、method等

@RequestMapping(value = "/getAllUrl1", method = RequestMethod.POST)
    public Object getAllURL() {
        List<Map<String, String>> resultList = new ArrayList<>();

        RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();

        for (Map.Entry<RequestMappingInfo, HandlerMethod> mappingInfoHandlerMethodEntry : map.entrySet()) {
            Map<String, String> resultMap = new LinkedHashMap<>();

            RequestMappingInfo requestMappingInfo = mappingInfoHandlerMethodEntry.getKey();
            HandlerMethod handlerMethod = mappingInfoHandlerMethodEntry.getValue();

            resultMap.put("className",handlerMethod.getMethod().getDeclaringClass().getName()); // 类名
            Annotation[] parentAnnotations = handlerMethod.getBeanType().getAnnotations();
            for (Annotation annotation : parentAnnotations) {
                if (annotation instanceof Api) {
                    Api api = (Api) annotation;
                    resultMap.put("classDesc",api.value());
                } else if (annotation instanceof RequestMapping) {
                    RequestMapping requestMapping = (RequestMapping) annotation;
                    if (null != requestMapping.value() && requestMapping.value().length > 0) {
                        resultMap.put("classURL",requestMapping.value()[0]);//类URL
                    }
                }
            }
            resultMap.put("methodName", handlerMethod.getMethod().getName()); // 方法名
            Annotation[] annotations = handlerMethod.getMethod().getDeclaredAnnotations();
            if (annotations != null) {
                // 处理具体的方法信息
                for (Annotation annotation : annotations) {
                    if (annotation instanceof ApiOperation) {
                        ApiOperation methodDesc = (ApiOperation) annotation;
                        String desc = methodDesc.value();
                        resultMap.put("methodDesc",desc);//接口描述
                    }
                }
            }
            PatternsRequestCondition p = requestMappingInfo.getPatternsCondition();
            for (String url : p.getPatterns()) {
                resultMap.put("methodURL",url);//请求URL
            }
            RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
            for (RequestMethod requestMethod : methodsCondition.getMethods()) {
                resultMap.put("requestType",requestMethod.toString());//请求方式:POST/PUT/GET/DELETE
            }
            resultList.add(resultMap);
        }
        return resultList;

    }

在这里插入图片描述
二.单纯只导出url

@RequestMapping("getAllUrl")
    @ResponseBody
    public Set<String> getAllUrl(HttpServletRequest request) {
        Set<String> result = new HashSet<String>();
        WebApplicationContext wc = (WebApplicationContext) request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        RequestMappingHandlerMapping bean = wc.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = bean.getHandlerMethods();
        for (RequestMappingInfo rmi : handlerMethods.keySet()) {
            PatternsRequestCondition pc = rmi.getPatternsCondition();
            Set<String> pSet = pc.getPatterns();
            result.addAll(pSet);
        }
        return result;
    }

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以通过HTTPS方式调用RESTful接口。在Spring Boot中,可以使用Spring的WebClient来实现HTTPS调用。WebClient提供了许多用于发送HTTP请求的方法,包括支持HTTPS的方法。 首先,需要在Spring Boot的配置文件中配置HTTPS的相关参数,包括证书、密钥等。可以使用Java的KeyStore来管理证书和密钥。在配置文件中设置相关参数,以告诉Spring Boot要使用HTTPS。 然后,在代码中使用WebClient来发送HTTPS请求。首先,需要创建一个WebClient的实例,并设置其相关参数,包括HTTPS的验证和超时时间。然后,使用WebClient发送HTTP请求,并处理响应结果。 以下是一个简单的示例代码,用于使用Spring Boot的WebClient发送HTTPS请求调用RESTful接口: ```java import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.client.WebClient; public class Main { public static void main(String[] args) { // 创建WebClient实例 WebClient client = WebClient.builder() .baseUrl("https://api.example.com") .build(); // 发送HTTPS请求 String response = client.method(HttpMethod.GET) .uri("/restful/api") .accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToMono(String.class) .block(); // 处理响应结果 System.out.println(response); } } ``` 以上代码示例中,首先创建了一个WebClient实例,并设置其baseUrl为目标RESTful接口的地址。然后使用method、uri方法设置请求的相关参数。最后,使用retrieve方法发送请求,并使用bodyToMono方法将响应结果转换为String类型。 通过以上的步骤,就可以使用Spring Boot的WebClient来通过HTTPS方式调用RESTful接口了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值